【问题标题】:How to update any specific MKAnnotationView image on click button from NavigationBar?如何在 NavigationBar 的单击按钮上更新任何特定的 MKAnnotationView 图像?
【发布时间】:2017-10-25 03:59:24
【问题描述】:

我在 Map 中添加了一些带有 init 方法的 annotationViews(由那里的 id 初始化)。现在我想更新导航栏点击按钮上的特定 id 注释视图。

假设我添加了 5 个 ID 为(1、2、3、4 和 5)的注释

从 VC 添加:

let annotation = MapPinAnnotation(title: storeItem.name!, location: CLLocationCoordinate2DMake(Double(lat), Double(long)), id: storeItem.storeId!)
self.mapview.addAnnotation(annotation)

初始化 AnnotationView:

class MapPinAnnotation: NSObject, MKAnnotation {

    var title:String?
    var id:String?
    private(set) var coordinate = CLLocationCoordinate2D()

    init(title newTitle: String, location: CLLocationCoordinate2D, id: String) {
        super.init()

        self.title = newTitle
        self.coordinate = location
        self.id = id
    }
}

ViewFor注解方法:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if (annotation is MKUserLocation) {
            return nil
        }
        if (annotation is MapPinAnnotation) {
            let pinLocation = annotation as? MapPinAnnotation
            // Try to dequeue an existing pin view first.
            var annotationView: MKAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: "MapPinAnnotationView")
            if annotationView == nil {
                annotationView?.image = UIImage(named: Constants.Assets.PinGreen)
            }
            else {
                annotationView?.annotation = annotation
            }
            return annotationView
        }
        return nil
    }

现在我想在导航栏的点击按钮上更改注释视图(id 4)的图像。

如何更新?请帮忙。 提前致谢。

【问题讨论】:

    标签: swift3 mapkit mkannotationview


    【解决方案1】:

    您可以使用view(for: ) 方法获取特定的MKAnnotationView。试试下面的代码:

    func clickButton() {
        for annotation in self.mapView.annotations {
            if annotation.id == 4 {
                let annotationView = self.mapView.view(for: annotation)
                annotationView?.image = UIImage(named: "Image name here")
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      相关资源
      最近更新 更多