1

I have POI's in Dictionary:

["_id": "12345ACB", "title": "Some Shop Place", "category": "Shop", "latitude": 12.345677, "longitude": 21.12348], ... 

and I have function for adding annotations:

func addPlaces() {
    for poi in pois {
        let pin = MKPointAnnotation()
        if let loc = poi["location"] as? NSDictionary {
            pin.coordinate = CLLocationCoordinate2D(latitude: (loc["lat"] as? Double)!, longitude: (loc["lng"] as? Double)!)
        }
        if let cat = poi["_category"] as? NSDictionary {
            pin.subtitle = cat["name"] as? String
        }
        pin.title = poi["title"] as? String
        mapView.addAnnotation(pin)

    }
}

and also a callout function:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    let info = view.annotation
    let placeName = info?.title
    let placeInfo = info?.subtitle
    let ac = UIAlertController(title: placeName!, message: placeInfo!, preferredStyle: .actionSheet)
    ac.addAction(UIAlertAction(title: "Open", style: .default, handler: { (UIAlertAction) in
        // Here I need POI's _ID
        self.performSegue(withIdentifier: "DealsSegue", sender: self)
    }))
    present(ac, animated: true)
}

So, in action for alert I need _id from POI's dictionary for specific (tapped) pin?

Thanks

miff
  • 185
  • 4
  • 15
  • 1
    You may use this http://stackoverflow.com/questions/38716410/add-extra-detail-to-mkpointannotation-other-than-title-and-subtitle to add extra information like the `_id` value to it, retrieve it in `mapView(_mapView:annotationView:calloutAccesoryControlTapped:)`, and instead of putting `self` in `sender` of `performSegue(withIdentifier:)` giving it. – Larme May 11 '17 at 12:14
  • Great, thanks. This is a solution (instruction from [question](http://stackoverflow.com/questions/38716410/add-extra-detail-to-mkpointannotation-other-than-title-and-subtitle)), and in a mapView:annotationView:callout... function view.annotation cast to MyAnnotation `let info = view.annotation as! MyAnnotation` – miff May 11 '17 at 12:44

1 Answers1

0
var catId: String? // catId objects in MKPointAnnotation Class

pin.catId = cat["_id"] as? String 
let placeId = info?.catId
let alertStr  =   String(format: "%@ %@", placeId!, placeInfo!,) // Your id and Your message 

let ac = UIAlertController(title: placeName!, message: alertStr, preferredStyle: .actionSheet)
ac.addAction(UIAlertAction(title: "Open", style: .default, handler: { (UIAlertAction) in
    // Here I need POI's _ID
    self.performSegue(withIdentifier: "DealsSegue", sender: self)
}))
Lalit kumar
  • 1,797
  • 1
  • 8
  • 14