设置注释时获得nil。 mapitems的价值,mapkit swift 4

时间:2018-05-25 18:33:57

标签: swift mapkit

我正在使用Xcode 9和mapkit在swift 4中创建一个应用程序。现在,我希望用户点击某些按钮,然后在地图上弹出对应于单击按钮的注释。示例:单击“餐厅”按钮,以便在给定区域中的所有餐厅弹出注释。

将注释添加到我的mapview时会出现问题。我得到一个致命的错误:在展开时发现零。这是代码:

enum annotations : String {
case restaurants = "Restaurants"
case movieTheater = "Movie Theater"
case shopping = "Shopping"
case bowling = "Bowling"
case park = "Park"
}

//In viewcontroller.swift
var annotationPoints : [MKMapItem] = []

@IBAction func annotationClicked(_ sender: UIButton) {
    guard let title = sender.currentTitle, let annotation =       annotations(rawValue: title) else {
        return
    }
    //only added two cases to test
    switch annotation {
    case .restaurants:
        getAnnotations(query: annotation.rawValue)
    default:
        getAnnotations(query: annotation.rawValue)
    }
}

func getAnnotations(query : String) {
    print("creating request")

    let request  = MKLocalSearchRequest()
    print("setting request")
    request.naturalLanguageQuery = query

    let span = MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)
    let region = MKCoordinateRegion(center: (mapView.userLocation.location?.coordinate)!, span: span)
    print("setting region")
    request.region = region
    print("creating search")
    let search = MKLocalSearch(request: request)
    print("starting search")
    search.start { (response, error) in
        guard let response = response else {
            return
        }
        self.annotationPoints = response.mapItems
        var annotation : GetAnnotationPins?

        print(self.annotationPoints)
        for points in self.annotationPoints {
            print("the points name is \(String(describing: points.name))")
            print("the points number  is \(String(describing: points.phoneNumber))")
            print("the points coordinates are \(points.placemark.coordinate)")

            annotation?.title = points.name
            print("the title is \(String(describing: annotation?.title))")

            annotation?.subtitle = points.phoneNumber
            print("the subtitle is \(String(describing: annotation?.subtitle))")

            annotation?.coordinate = points.placemark.coordinate
            print("the coordinates are \(String(describing: annotation?.coordinate))")

            print("the annotaiton is \(String(describing: annotation))")


            self.mapView.addAnnotation(annotation!)

        }



//separate class for pin annotation 

class GetAnnotationPins : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var subtitle: String?
var title: String?

init(coordinate : CLLocationCoordinate2D, subtitle : String, title : String) {
    self.coordinate = coordinate
    self.subtitle = subtitle
    self.title = title
}

}

//console print out
the points name is Optional("Lin\'s Garden") // restaurant near my location 
the points number  is Optional("‭+1 (715) 693-8899‬")
the points coordinates are CLLocationCoordinate2D(latitude:        44.791603000000002, longitude: -89.700630000000004)
the title is nil
the subtitle is nil
the coordinates are nil
the annotaiton is nil

1 个答案:

答案 0 :(得分:0)

您应该初始化app.post('/pushtransaction', function(req, res) { console.log(req.body); console.log(5); if (req.body.sigs) { let sigver = xmf.modules.ecc.Signature.from(req.body.sigs).toString(); let lasig = [sigver]; console.log(req.body.packedTr); let transi = JSON.parse(req.body.packedTr); //let sigver = req.body.sigs; let package = { compression: 'none', transaction: transi, signatures: lasig } console.log(package); //Pushes tx in correct format xmf.pushTransaction(package).then(result=>{ res.send(result); res.end(); console.log(result); }).catch(err => { console.log(err) }); } }) 课程。请尝试以下代码:

GetAnnotationPins

并替换

for points in self.annotationPoints {
    var annotation = GetAnnotationPins(coordinate: points.placemark.coordinate,
        subtitle: points.phoneNumber,
        title: points.name)
    ....
}

 self.mapView.addAnnotation(annotation!)