使 Mapbox 注释根据变量具有不同的图像?

问题描述

所以我在更新地图视图函数中按如下方式在 for 循环中创建我的注释:

for item in array {
    let latitude = item.latitude
    let longitude = item.longitude
    let coordinate = CLLocationCoordinate2D(latitude: latitude,longitude: longitude)
    let annotation = //make an annotation that uses a different image in Assets depending on a variable called "imageID"
    mapView.addAnnotation(annotation)
}

我尝试过使用重用标识符,但还没有接近解决这个问题。我还尝试使用 imageFor 函数并制作自定义注释类(以检测传入的图像并相应地更改视图)但它没有用:

class CustomAnnotation: NSObject,MGLAnnotation {

    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?

    var image: UIImage?

    init(coordinate: CLLocationCoordinate2D,title: String?,subtitle: String?) {
        self.coordinate = coordinate
        self.title = title
        self.subtitle = subtitle
    }
}

func mapView( _ mapView: MGLMapView,imageFor annotation: MGLAnnotation ) -> MGLAnnotationImage? {
        
    let annotationImage: MGLAnnotationImage
    let annotationImagetoilet = mapView.dequeueReusableAnnotationImage(withIdentifier: "custom1")
    let annotationImageDefault = mapView.dequeueReusableAnnotationImage(withIdentifier: "defaultimage")
            
    switch annotation.image { //tried to sort according to the image value of the custom class
        case "custom1":
            var featureAnnotation = FeatureAnnotation(coordinate: annotation.coordinate,title: "Title",subtitle: "subtitle")
            featureAnnotation.image = UIImage(named: "custom1")!
            annotationImage = annotationImagetoilet ?? featureAnnotation //Couldn't get this line working properly
        default:
            var featureAnnotation = FeatureAnnotation(coordinate: annotation.coordinate,subtitle: "subtitle")
            featureAnnotation.image = UIImage(named: "defaultimage")!
            annotationImage = annotationImageDefault ?? featureAnnotation //Couldn't get this line working properly
        }
            
        return annotationImage
 }

本质上,我只希望自定义注释的图像根据 feature.imageID 的不同而改变,非常感谢任何帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)