2016-09-22 25 views
0

我想给我的两个MKPointAnnotation针自定义图像。到目前为止,我可以在两个引脚上显示相同的图像。我可以对下面的代码做些什么来给每个pin一个自定义图像?多个自定义点注释 - 斯威夫特

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

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


     let annotationIdentifier = "AnnotationIdentifier" 

     var annotationView: MKAnnotationView? 

     if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) { 
      annotationView = dequeuedAnnotationView 
      annotationView?.annotation = annotation 
     } 
     else { 
      annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
      annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) 
     } 


     if let annotationView = annotationView { 
      annotationView.canShowCallout = true 
      annotationView.image = UIImage(named: "Moto_pin") 
     } 

     return annotationView 
    } 

回答

0

而不是annotationView.image = UIImage(named: "Moto_pin"),您将需要更改每个引脚的图像。该行将为每个注释视图分配相同的图像。

+0

但是我怎么访问examplePoint = MKPointAnnotation()形象? – cosmos

0

尝试使用MKPinAnnotationView而不是MKAnnotationView。为此,您只需更改:

var annotationView:MKPinAnnotationView?

有了这个类,你可以定制你的脚色,整个视图

+0

我正在尝试使用一个图像不会变色的针。 – cosmos