2017-10-14 99 views
-1

我自定義我的callOutView,創建自己的AnnotationViewtheGreatAnnotationView,但我想保留在地圖上的標準註釋針。在此代碼中,我使用了自定義註釋圖像view?.image = annotation.image,但何時刪除該行,我的地圖上沒有註釋。你能幫我解決這個問題嗎?如何使用標準MKAnnotation符號與自定義MKAnnotationView

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

    var annoView: MKAnnotationView? 
    if annotation.isKind(of: MKUserLocation.self) { 

     return nil 
    } 



    var view = surroundingMapView.dequeueReusableAnnotationView(withIdentifier: "imageAnnotation") 
    if view == nil { 
     view = theGreatAnnotationView(annotation: annotation, reuseIdentifier: "imageAnnotation") 
     reuseIdentifier: "imageAnnotation") 
     view!.canShowCallout = false 

    } 

    let annotation = annotation as! Artwork 


    view?.image = annotation.image 
    view?.annotation = annotation 


    return view 
} 
+0

如果你想在你的地圖上使用標準的pin而不是'image',你應該使用'MKPinAnnotationView'。 – Rob

回答

0

您應該使用MKPinAnnotationView代替MKAnnotationView

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

    var annoView: MKAnnotationView? 
    if annotation.isKind(of: MKUserLocation.self) { 

     return nil 
    } 

    if /* if annotation is image */ { 
     var view = surroundingMapView.dequeueReusableAnnotationView(withIdentifier: "imageAnnotation") 
     if view == nil { 
      view = theGreatAnnotationView(annotation: annotation, reuseIdentifier: "imageAnnotation") 
      view!.canShowCallout = false 

     } 

     let annotation = annotation as! Artwork 


     view?.image = annotation.image 
     view?.annotation = annotation 
    } else { 
     /* if annotation is pin */ 
     var view = surroundingMapView.dequeueReusableAnnotationView(withIdentifier: "pinAnnotation") 
     if view == nil { 
      view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pinAnnotation") 
      view!.canShowCallout = false 
     } 

     let annotation = annotation as! Artwork 


     view?.annotation = annotation 
    } 

    return view 
} 
+0

謝謝你的回答。我想爲每個註釋使用我的theGreatAnnotationView(因爲註釋的callOutView),但仍然需要每個註釋的標註註釋圖像。所以沒有兩種情況。 – TheRJI