为什么我的自定义注释图像没有显示?

Why is my custom annotation image not showing up?

我正在尝试通过 viewForAnnotation 方法更改图钉的图像。这是我的代码:

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

    if annotation.isMemberOfClass(MKUserLocation.self) {
        return nil
    }

    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = false
        pinView!.image = UIImage(named: "marker.png")
    }
    else {
        pinView!.annotation = annotation
    }

    return pinView

}

当我运行项目时,图像不显示。我做错了什么?

试试这个代码会有帮助。

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

if annotation.isMemberOfClass(MKUserLocation.self) {
    return nil
}

let reuseId = "MKAnnotationView"

var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKAnnotationView
if pinView == nil {
    pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)}
    pinView!.canShowCallout = true
    pinView!.animatesDrop = false // i am not sure about this line so must try this by commenting
    pinView!.image = UIImage(named: "marker.png") // make sure this name is the name of image

return pinView

}

会有帮助。