Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Mapbox iOS SDK上动态更改MGLPointAnnotation图像_Ios_Objective C_Swift_Cocoa Touch_Mapbox - Fatal编程技术网

在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)
,但无效

知道我该怎么做吗

谢谢

  • 我很确定,在将is添加到地图后,无法更改注释(除非它是annotationView)

  • 只有在添加批注时才会调用
    mapView(mapView,imageForAnnotation批注)

  • 您可以做的是使用didselect delegete

    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。。?