Jake Ols
Jake Ols

Reputation: 2852

MKMapView Select Annotation

I'm new to swift and currently trying to figure out how to get data about the annotation that the user has selected. I have a localsearch function that will add the annotations, and after the user selects one I'd like to be able to access that. I'm trying to use selectedAnnotations, but it doesn't seem to be working.

Localsearch:

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)

            }
        }
    })

From there I'm trying to use

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

To access the annotation, but this is just returning "nil"

Method for annotation:

    @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)





    }

Upvotes: 0

Views: 2411

Answers (1)

user467105
user467105

Reputation:

Here's an example of how to use the selectedAnnotations property:

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...
    }
}

(Though whether you need to or want to do this inside your // has been saved block instead of outside is something you'll have to figure out.)

Upvotes: 1

Related Questions