在Mapbox iOS SDK上动态更改MGLPointAnnotation图像
我有一个应用程序,它在其在Mapbox iOS SDK上动态更改MGLPointAnnotation图像,ios,objective-c,swift,cocoa-touch,mapbox,Ios,Objective C,Swift,Cocoa Touch,Mapbox,我有一个应用程序,它在其iosdk上使用Mapbox地图,并在上面显示标记(MGLPointAnnotation) 我想在选择标记时更改标记的图像 MGLPointAnnotation没有image属性,我尝试调用委托方法mapView(mapView,imageForAnnotation annotation),但无效 知道我该怎么做吗 谢谢 我很确定,在将is添加到地图后,无法更改注释(除非它是annotationView) 只有在添加批注时才会调用mapView(mapView,image
iosdk
上使用Mapbox
地图,并在上面显示标记(MGLPointAnnotation
)
我想在选择标记时更改标记的图像
MGLPointAnnotation
没有image
属性,我尝试调用委托方法mapView(mapView,imageForAnnotation annotation)
,但无效
知道我该怎么做吗
谢谢
mapView(mapView,imageForAnnotation批注)
func映射视图(u映射视图:MGLMapView,didSelect注释视图:MGLAnnotationView){//code
}
func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
//code
}
下面是一个客户标记图像的示例:当您单击其他图像时,我设法更改了图像
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
}
我认为调用mapView.removeAnnotation(myAnnotation),然后立即调用mapView.addAnnotation(myAnnotation)会迫使它再次调用imageForAnnotation。。?