使自己MKAnnotation
类,其中(一)coordinate
是不是只读的;并在(B)这是dynamic
,例如:
class MyAnnotation: NSObject, MKAnnotation {
dynamic var coordinate: CLLocationCoordinate2D
dynamic var title: String?
dynamic var subtitle: String?
init(coordinate: CLLocationCoordinate2D, title: String? = nil, subtitle: String? = nil) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
super.init()
}
}
然后用这个来代替MKPointAnnotation
。显然,您应该使用比MyAnnotation
更具描述性的名称。
此外,当您移动它们,你真的应该将它们转换成任何你叫你的新的注释类:
for existingMarker in self.mapView.annotations {
if let annotation = existingMarker as? MyAnnotation {
annotation.coordinate = CLLocationCoordinate2D(latitude: 12.12121212, longitude: 14.312121121)
}
}
或者
self.mapView.annotations.flatMap({ $0 as? MyAnnotation}).forEach {
$0.coordinate = CLLocationCoordinate2D(latitude: 12.12121212, longitude: 14.312121121)
}
来源
2017-05-28 23:16:51
Rob