如何将标准MKAnnotation符号与定制的MKAnnotationView一起使用

时间:2017-10-14 17:09:53

标签: swift mapkit mkannotationview

我自定义了callOutView,创建了自己的AnnotationView theGreatAnnotationView,但我希望在地图上保留标准的注释图钉。在此代码中,我使用自定义注释图像view?.image = annotation.image,但是当删除该行时,我的地图上没有注释。你能帮我解决这个问题吗?

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
}

1 个答案:

答案 0 :(得分: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
}