How to determine which annotation was selected in MapView

I made some annotations inside the Map, and when I click on them, I see some information, and I have a button that opens the Maps, and with the correct information that I can’t take, I should draw my route to me.

Here is my code:

I have 2 double arrays for my lat and lon. I filled them out from my request.

var lat = [Double]() var lon = [Double]() 

These lines are for filling annotations

 self.annot = Islands(title: object["name"] as! String, subtitle: object["address"] as! String, coordinate: CLLocationCoordinate2D(latitude: (position?.latitude)!, longitude: (position?.longitude)!), info: object["address"] as! String) self.map.addAnnotations([self.annot]) 

An annotation is created here:

 func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { let identifier = "pin" if annotation.isKindOfClass(MKUserLocation) { return nil } let image = UIImage(named: "car") let button = UIButton(type: .Custom) button.frame = CGRectMake(0, 0, 30, 30) button.setImage(image, forState: .Normal) button.addTarget(self, action: #selector(Map.info(_:)), forControlEvents:UIControlEvents.TouchUpInside) var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) if annotationView == nil { annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin") annotationView!.canShowCallout = true annotationView!.image = UIImage(named: "annotation") annotationView!.rightCalloutAccessoryView = button } else { annotationView!.annotation = annotation } return annotationView } 

And finally, this is the function of the button, where should I transmit the correct information (and where is the problem)

  func info(sender: UIButton) { let latitute:CLLocationDegrees = lat[sender.tag] let longitute:CLLocationDegrees = lon[sender.tag] let regionDistance:CLLocationDistance = 10000 let coordinates = CLLocationCoordinate2DMake(latitute, longitute) let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance) let options = [ MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span) ] let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil) let mapItem = MKMapItem(placemark: placemark) mapItem.name = name[sender.tag] print(sender.tag) mapItem.openInMapsWithLaunchOptions(options) } 

As you can see, I tried with the sender tag, but no luck.

EDIT: My custom annotation class

 class Islands: NSObject, MKAnnotation { var title: String? var addr: String? var coordinate: CLLocationCoordinate2D var info: String? var subtitle: String? init(title: String, subtitle: String, coordinate: CLLocationCoordinate2D, info: String) { self.title = title self.coordinate = coordinate self.info = info self.subtitle = subtitle } } 
+8
dictionary ios annotations swift
source share
3 answers

To do this, you can use the selected Annotation from didSelectAnnotationView , then save this annotation in the instance variable and after that use the annotation in your Button action method.

 var selectedAnnotation: MKPointAnnotation? func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { self.selectedAnnotation = view.annotation as? MKPointAnnotation } func info(sender: UIButton) { print(selectedAnnotation?.coordinate) } 

Edit:. You have a custom MKAnnotation class that you need to use.

 var selectedAnnotation: Islands? func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { self.selectedAnnotation = view.annotation as? Islands } 
+7
source share

For Swift 4

 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { var selectedAnnotation = view.annotation } 
+3
source share

hey man please tell me what you did because i have the same problem thanks

0
source share

All Articles