如何创建自定义 MKMarkerAnnotationView 图像?
How to create custom MKMarkerAnnotationView image?
我想在带有自定义图像的 mapView 上创建标记。是否可以同时显示图像和标题?我尝试了几种方法
- MKMarkerAnnotation 视图。使用 .image 它设置图像,但默认气泡仍然存在,使用 .glyphImage 我无法让图像保留原始颜色
- MKPin 注释。我没有显示标题。
我需要它们都与 MKMarkerAnnotationView 同时显示,但带有自定义图钉图像。我该怎么做才能做到这一点?
它适用于自定义 class 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)
}
我想在带有自定义图像的 mapView 上创建标记。是否可以同时显示图像和标题?我尝试了几种方法
- MKMarkerAnnotation 视图。使用 .image 它设置图像,但默认气泡仍然存在,使用 .glyphImage 我无法让图像保留原始颜色
- MKPin 注释。我没有显示标题。
我需要它们都与 MKMarkerAnnotationView 同时显示,但带有自定义图钉图像。我该怎么做才能做到这一点?
它适用于自定义 class 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)
}