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