2016-09-22 37 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