【发布时间】: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