如何创建自定义MKMarkerAnnotationView图像?

问题描述 投票:-2回答:1

我想在mapView上用自定义图像创建一个标记。是否可以同时显示图像和标题?我试过几种方法

  • MKMarkerAnnotation视图。使用.image可以设置图像,但默认的气泡仍然存在,使用.glyphImage我无法让图像保留原来的颜色。
  • MKPinAnnotation。我得到的是不显示标题。

我需要将它们两个都与MKMarkerAnnotationView同时显示,但要有一个自定义的引脚图像。我可以做些什么来实现这一点?

ios swift mapkit
1个回答
1
投票

它的工作与自定义类mkAnnotation,我不知道它是最好的做法或没有,但它在我的应用程序中的工作原理。

class SightMap: NSObject, MKAnnotation {
     let identifier = "pinImage"
     //+ something else, for example 
     var image: UIImage?
}
///somewhere in your code
     let sightmap = SightMap(...init...)
     map?.addAnnotation(sightmap)

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let view = MKAnnotationView(annotation: annotation, reuseIdentifier: "pinImage")
        view.addSubview(YOUR_IMAGE)
        view.addSubview(YOUR_label)

    }
© www.soinside.com 2019 - 2024. All rights reserved.