【问题标题】:How to use standard MKAnnotation Symbol with a custom made MKAnnotationView如何将标准 MKAnnotation Symbol 与定制的 MKAnnotationView 一起使用
【发布时间】:2017-10-14 17:09:52
【问题描述】:

我自定义了我的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
}

【问题讨论】:

  • 如果您想在地图上使用标准图钉而不是image,您应该使用MKPinAnnotationView

标签: swift mapkit mkannotationview


【解决方案1】:

您应该使用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
}

【讨论】:

  • 感谢您的回答。我想为每个注释使用我的 theGreatAnnotationView(因为注释的 callOutView ),但仍然需要每个注释的标准注释 Pin 图像。所以没有两种情况。
猜你喜欢
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
  • 1970-01-01
  • 1970-01-01
  • 2015-09-17
  • 1970-01-01
相关资源
最近更新 更多