在Mapbox iOS SDK上动态更改MGLPointAnnotation图像 [英] Dynamically change MGLPointAnnotation image on Mapbox iOS SDK

查看:312
本文介绍了在Mapbox iOS SDK上动态更改MGLPointAnnotation图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序在其iOS SDK上使用Mapbox的映射,并在其上显示标记(MGLPointAnnotation).

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没有image属性,我尝试调用委托方法mapView(mapView, imageForAnnotation annotation),但没有用.

MGLPointAnnotation has no image property and I've tried to call the delegate method mapView(mapView, imageForAnnotation annotation) but it didn't work.

任何想法我该怎么做?

谢谢!

推荐答案

当您单击其他图像时,我设法更改了图像.

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
    }

这篇关于在Mapbox iOS SDK上动态更改MGLPointAnnotation图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆