2016-04-21 39 views
1

我想在地圖上自定義註釋,而不是針腳,我希望註釋看起來像當前的位置指示器。我將如何做到這一點?任何建議都會很棒!如何使註釋看起來像當前位置指示符?

+0

只是一個圓點?爲它創建一個圖像,然後讓'viewForAnnotation'創建一個'MKAnnotationView'並設置它的'image'屬性。 – Rob

+0

請參閱[定義自定義註釋對象](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html#//apple_ref/doc/uid/TP40009497-CH6- SW2)在_Location和地圖編程指南._ – Rob

回答

1

一般來說,您可以使用擴展MKPointAnnotation的自定義註釋對象。但是,如果你只需要改變圖釘圖像就可以避免子類,只是實現這個方法

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

    let identifier = "MyPin" 

    if annotation.isKindOfClass(MKUserLocation) { 
     return nil 
    } 

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 

    if let annotationView = annotationView { 
     annotationView.annotation = annotation 
    } else { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
     annotationView.image = UIImage(named: "myPinImage") 
    } 

    return annotationView 
} 

所以,這足以讓你找到正確的圖像,並用它替代引腳。 但是,如果你想跟着默認currentLocation圖像可能發生的變化,你可以重複使用默認視圖

let annotationView = mapView.viewForAnnotation(mapView.userLocation()); 
+0

我得到這個錯誤http://puu.sh/orVH6/065e730604.png – user6032625

+0

@ user6032625 - 他的意思是如果讓'='而不是'=='。 – Rob