Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i tag the annotation in the map and get the tag value while selecting the annotation

Tags:

ios

swift

swift3

Iam doing a project for deliver food online through map. so the user should know the cook location. When the user tap on the annotation, it should view the food menus of the cook. so when user tap i need to call the cooks id. I can can call the cooks id according to the tag value.

**Juntos.swift**


func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> 
MKAnnotationView?
{
    var annotationView = 
mapView.dequeueReusableAnnotationView(withIdentifier: "identifier")

    if annotationView == nil
      {
        annotationView = MKAnnotationView(annotation: annotation, 
         reuseIdentifier: "identifier")
        annotationView!.canShowCallout = true
      }
    else
      {
        annotationView?.annotation = annotation
      }

    guard !(annotation is MKUserLocation) else
      {
        return nil
      }

    if iamA == "COOK"
      {
        annotationView!.image = UIImage(named: "foodie")
      }
    else
      {
        annotationView!.image = UIImage(named: "cook")
      }
           return annotationView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
{
    //    how can i get annotation tag here
}
like image 332
Wide Angle Technology Avatar asked Dec 23 '22 15:12

Wide Angle Technology


1 Answers

subclass MKAnnoation and enable it to hold custom data... e.g. the id:

class MyAnnotation: MKAnnotation {
    var identifier: String!
}

now add that instead of MKAnnotation

let a = MyAnnotation()
....
a.identifier = "id525325"
mapView.addAnnotation(a)

and in then later use it

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    guard let a = view.annoation as? MyAnnoation else {
        return
    }
    let identifier = a.identifier
like image 66
Daij-Djan Avatar answered Mar 30 '23 01:03

Daij-Djan