我的问题是,当我在mapbox mapview中选择一个注释时,状态更改为selected,为此注释的图像我编写了此委托方法
func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) {
if let castAnnotation = annotation as? MyCustomPointAnnotation {
if (!castAnnotation.willUseImage) {
return
}
for mapAnn in mapView.annotations!
{
castAnnotation.selected = false
if ((mapAnn.coordinate.longitude == annotation.coordinate.longitude) && (mapAnn.coordinate.latitude == annotation.coordinate.latitude))
{
castAnnotation.selected = true
castAnnotation.willUseImage = true
mapView.removeAnnotation(annotation)
mapView.addAnnotation(castAnnotation)
}
}
}
}
addAnnotation方法触发此imageForAnnotation方法: func mapView(_ mapView:MGLMapView,imageFor注释:MGLAnnotation)-> MGLAnnotationImage? {
if let castAnnotation = annotation as? MyCustomPointAnnotation {
if (!castAnnotation.willUseImage) {
return nil;
}
if (!castAnnotation.selected == true) {
var annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: String(describing: annotation.coordinate))
annotationImage = MGLAnnotationImage(image: UIImage(named: "pin_deselected")!, reuseIdentifier: String(describing: annotation.coordinate))
annotationImage?.image = textToImage(drawText: castAnnotation.stepId, inImage: (annotationImage?.image)!, atPoint: CGPoint(x: 10, y: 5))
return annotationImage!
}else{
var annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: String(describing: annotation.title))
if(annotationImage == nil) {
annotationImage = MGLAnnotationImage(image: UIImage(named: "pin_selected")!, reuseIdentifier: String(describing: annotation.coordinate))
}
annotationImage?.image = textToImage(drawText: castAnnotation.stepId, inImage: (annotationImage?.image)!, atPoint: CGPoint(x: 10, y: 5))
return annotationImage!
}
}
return nil
}
在第二种情况下可以正确触发,但管脚图像没有改变!!!!! 我不知道为什么?