使自己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