[英]Dynamically change MGLPointAnnotation image on Mapbox iOS SDK
I have an app that uses a map of Mapbox
on its iOS SDK
and present markers ( MGLPointAnnotation
) on it. 我有一个应用程序,它在iOS SDK
上使用Mapbox
地图, Mapbox
在其上显示标记( MGLPointAnnotation
)。
I want to change the image of a marker when it selected. 我想在选择时更改标记的图像。
MGLPointAnnotation
has no image
property and I've tried to call the delegate method mapView(mapView, imageForAnnotation annotation)
but it didn't work. MGLPointAnnotation
没有image
属性,我试图调用委托方法mapView(mapView, imageForAnnotation annotation)
但它不起作用。
Any idea how can I do that? 知道我该怎么办?
Thanks! 谢谢!
I'm pretty sure that there is no way to change an annotation after is has been added to the map (except it is an annotationView) 我很确定在添加到地图后没有办法更改注释(除了它是annotationView)
The mapView(mapView, imageForAnnotation annotation)
gets only called, when you add an annotation. 只有在添加注释时才会调用mapView(mapView, imageForAnnotation annotation)
。
What you can do is to to use the didselect delegete 你可以做的是使用didselect delegete
func mapView(_ mapView: MGLMapView, didSelect annotationView: MGLAnnotationView){ //code }
And set there a new annotation at the same location but this time with your desired image. 并在同一位置设置一个新的注释,但这次是您想要的图像。 Then the image annotation delegate gets called, where you define your image 然后调用图像注释委托,您可以在其中定义图像
func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
//code
}
Here's an example for a customer marker image: https://www.mapbox.com/ios-sdk/examples/marker-image/ 以下是客户标记图像的示例: https : //www.mapbox.com/ios-sdk/examples/marker-image/
I managed to change the image when you click on a different image. 当您单击其他图像时,我设法更改了图像。
var poiSelected: MGLAnnotation?
private func printPoiImage(with name: String, annotation: MGLAnnotation, mapView: MGLMapView) {
let id = "\(annotation.coordinate.latitude)+\(annotation.coordinate.longitude)"
let annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: id)
annotationImage?.image = UIImage(named: name)
}
func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) {
if self.poiSelected == nil {
self.poiSelected = annotation
self.printPoiImage(with: "PoiOn", annotation: annotation, mapView: mapView)
}
}
func mapView(_ mapView: MGLMapView, didDeselect annotation: MGLAnnotation) {
self.poiSelected = nil
self.printPoiImage(with: "PoiOff", annotation: annotation, mapView: mapView)
}
func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
let id = "\(annotation.coordinate.latitude)+\(annotation.coordinate.longitude)"
var annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: id)
if annotationImage == nil {
var image: UIImage?
if self.poiSelected == nil {
image = UIImage(named: "PoiOff")
} else {
image = UIImage(named: "PoiOn")
}
guard var imageWrap = image else { return nil }
imageWrap = imageWrap.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: imageWrap.size.height/2, right: 0))
annotationImage = MGLAnnotationImage(image: imageWrap, reuseIdentifier: id)
}
return annotationImage
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.