Swift 注释自定义图片

Swift Annotation Custom Image

我正在使用 Swift 3 和 Xcode 10 beta 3,我需要为我在地图上的图钉使用自定义图像。在另一个人 post 的帮助下,我们编写了这段代码,除了引脚仍然是默认引脚外,一切正常。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if let annotation = annotation as? Annotation {
            let identifier = "identifier"
            let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView.image = annotation.image //add this
            annotationView.canShowCallout = true
            annotationView.calloutOffset = CGPoint(x: -5, y: 5)
            annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView
            return annotationView
        }
        return nil
    }

    let marker = Annotation(title: "LVR" , subtitle: "" , coordinate: CLLocationCoordinate2D(latitude: 43.772523, longitude: 11.254356))
    marker.image = UIImage(named: "antonioli.png")
    //mapView.addAnnotation(marker)
    self.map.addAnnotation(marker)

PNG 文件位于我项目的主文件夹中,如下所示:

我该如何处理这个问题?

这里MyAnnotationNSObject, MKAnnotation

的子类
func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView?
{
    if annotation is MyAnnotation == false
    {

        return nil
    }

    let senderAnnotation = annotation as! MyAnnotation

    let pinReusableIdentifier = senderAnnotation.pinColor.rawValue

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: pinReusableIdentifier)

    if annotationView == nil
    {
        annotationView = MKAnnotationView(annotation: senderAnnotation,                                                                                                reuseIdentifier: pinReusableIdentifier)
        annotationView!.canShowCallout = true

    }
    let pinImage = UIImage(named:"location_curr.png")

    annotationView!.image = pinImage

    return annotationView

}

查看演示 MapCustomImage