2017-02-13 34 views
1

我正在尝试制作很多不同类型的注释。所有注释都需要根据美丽的原因进行定制。如何在MKPointAnnotation中设置标识符

我知道它需要使用viewFor注解,但我怎么知道注释是什么样的?

enter image description here

func addZoneAnnotation() { 

    let zoneLocations = ZoneData.fetchZoneLocation(inManageobjectcontext: managedObjectContext!) 

    for zoneLocation in zoneLocations! { 

     let zoneCoordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: Double(zoneLocation["latitude"]!)!, longitude: Double(zoneLocation["longitude"]!)!) 

     let zoneAnnotation = MKPointAnnotation() 
     zoneAnnotation.coordinate = zoneCoordinate 


     map.addAnnotation(zoneAnnotation) 

    } 

} 
+0

zoneAnnotation.title =“YOURTITLE” –

+0

您可以使用viewForAnnotation委托,并根据注释的类型设置标签。 –

+1

检查此链接 - http://stackoverflow.com/questions/29307173/identify-mkpointannotation-in-mapview –

回答

0

子类MKPointAnnotation补充说,任何你想要的属性:

class MyPointAnnotation : MKPointAnnotation { 
    var identifier: String? 
} 

然后你可以使用它作为如下:

func addZoneAnnotation() { 
    let zoneLocations = ZoneData.fetchZoneLocation(inManageobjectcontext: managedObjectContext!) 

    for zoneLocation in zoneLocations! { 
     let zoneCoordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: Double(zoneLocation["latitude"]!)!, longitude: Double(zoneLocation["longitude"]!)!) 
     let zoneAnnotation = MyPointAnnotation() 
     zoneAnnotation.coordinate = zoneCoordinate 
     zoneAnnotation.identifier = "an identifier" 

     map.addAnnotation(zoneAnnotation) 
    } 
} 

最后,当您需要访问它:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    guard let annotation = annotation as? MyPointAnnotation else { 
     return nil 
    } 

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "reuseIdentifier") 
    if annotationView == nil { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "reuseIdentifier") 
    } else { 
     annotationView?.annotation = annotation 
    } 

    // Now you can identify your point annotation 
    if annotation.identifier == "an identifier" { 
     // do something 
    } 

    return annotationView 
} 
相关问题