MKMapView Select 注释

MKMapView Select Annotation

我是 swift 的新手,目前正在尝试了解如何获取有关用户选择的注释的数据。我有一个本地搜索功能,可以添加注释,在用户选择一个后,我希望能够访问它。我正在尝试使用 selectedAnnotations,但它似乎不起作用。

本地搜索:

func performSearch(){
    matchingItems.removeAll()
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = searchTextField.text
    request.region = mapView.region

    let search = MKLocalSearch(request: request)

    search.startWithCompletionHandler({(response:
        MKLocalSearchResponse!,
        error: NSError!) in

        if error != nil {
            println("Error occured in search: \(error.localizedDescription)")
        } else if response.mapItems.count == 0 {
            println("No matches found")
        } else {
            println("Matches found")

            for item in response.mapItems as [MKMapItem] {
                println("Name = \(item.name)")
                println("Phone = \(item.phoneNumber)")

                self.matchingItems.append(item as MKMapItem)
                println("Matching items = \(self.matchingItems.count)")

                var annotation = MKPointAnnotation()
                annotation.coordinate = item.placemark.coordinate
                annotation.title = item.name
                annotation.subtitle = item.placemark.title
                self.mapView.addAnnotation(annotation)

            }
        }
    })

从那里我正在尝试使用

 var selectedAnnotations: [MKPointAnnotation]!
        // print signout location
        println(selectedAnnotations)

访问注释,但这只是返回"nil"

标注方法:

    @IBAction func signoutToLocationButton(sender: AnyObject) {
    // saves current user location
    PFGeoPoint.geoPointForCurrentLocationInBackground {
        (geoPoint: PFGeoPoint!, error: NSError!) -> Void in
        if error == nil {
            // do something with the new geoPoint
            println(geoPoint)

            var signoutLocation = PFObject(className: "SignoutLocation")
            signoutLocation["Location"] = geoPoint
            signoutLocation.saveInBackgroundWithBlock {
                (success: Bool, error: NSError!)-> Void in
                if (success) {
                    // has been saved
                }
                else {
                    //went wrong
                }
            }

        }

        // get location of where they are signing out to
        self.mapView.selectedAnnotations(AnyObject)
        // print signout location
       //  println(selectedAnnotations)





    }

下面是如何使用 selectedAnnotations 属性 的示例:

if self.mapView.selectedAnnotations?.count > 0 {

    if let ann = self.mapView.selectedAnnotations[0] as? MKAnnotation {

        println("selected annotation: \(ann.title!)")

        let c = ann.coordinate
        println("coordinate: \(c.latitude), \(c.longitude)")

        //do something else with ann...
    }
}

(不过你是否需要或想要在你的 // has been saved 街区内而不是外面这样做是你必须弄清楚的事情。)