2015-10-17 78 views
1

爲了放置個人圖片而不是傳統紅色徽章,應該寫些什麼?更改圖片徽標地圖

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

    if annotation is MKUserLocation { 
     return nil 
    } 

    let annView : MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "currentloc") 
    annView.pinTintColor = UIColor.redColor() 
    annView.animatesDrop = true 
    annView.canShowCallout = true 
    annView.calloutOffset = CGPointMake(-8, 0) 

    annView.autoresizesSubviews = true 
    annView.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIView 

    return annView 
} 

回答

2

使用MKAnnotationView,而不是MKPinAnnotationView,然後設置其屬性image。我也建議實施出列邏輯,以便註釋可以重用:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    if annotation is MKUserLocation { 
     return nil 
    } 

    let annotationIdentifier = "SomeCustomIdentifier" // use something unique that functionally identifies the type of pin 

    var annotationView: MKAnnotationView! = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) 

    if annotationView != nil { 
     annotationView.annotation = annotation 
    } else { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 

     annotationView.image = UIImage(named: "annotation.png") 

     annotationView.canShowCallout = true 
     annotationView.calloutOffset = CGPointMake(-8, 0) 

     annotationView.autoresizesSubviews = true 
     annotationView.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIView 
    } 

    return annotationView 
} 
+0

annotationIdentifier它給了我錯誤。我將其替換爲:字符串:「隨機詞」 –

+0

是的,使用任何獨特的功能標識引腳的性質。對不起,如果我的意圖不清楚。我調整了上面的代碼示例。 – Rob

+0

謝謝:)友好和樂於助人 –