Dynamically change MGLPointAnnotation image on Mapbox iOS SDK

前提是你 提交于 2019-12-11 03:28:19

问题


I have an app that uses a map of Mapbox on its iOS SDK and present markers (MGLPointAnnotation) on it.

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.

Any idea how can I do that?

Thanks!


回答1:


  1. 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)

  2. The mapView(mapView, imageForAnnotation annotation) gets only called, when you add an annotation.

  3. What you can do is to to use the 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/




回答2:


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
    }


来源:https://stackoverflow.com/questions/40317861/dynamically-change-mglpointannotation-image-on-mapbox-ios-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!