Swift 如何使用MKAnnotation存储属性
我正在使用这条线创建带有自定义接点的地图Swift 如何使用MKAnnotation存储属性,swift,mkannotation,Swift,Mkannotation,我正在使用这条线创建带有自定义接点的地图 self.mapView.addAnnotation(customMapAnnotationView.annotation!) 但我需要稍后遍历所有管脚,并将它们与一个键匹配,该键将标识是否要删除它们 for annotation:ChatRoomMapAnnotationView in self.chatMapView.annotations { ... } 如何将该键与注释一起存储,以便在迭代时可用 谢谢如果我理解正确,如果您希望在注释中存储额
self.mapView.addAnnotation(customMapAnnotationView.annotation!)
但我需要稍后遍历所有管脚,并将它们与一个键匹配,该键将标识是否要删除它们
for annotation:ChatRoomMapAnnotationView in self.chatMapView.annotations {
...
}
如何将该键与注释一起存储,以便在迭代时可用
谢谢如果我理解正确,如果您希望在注释中存储额外信息,您需要创建一个符合MKAnnotation的类,如下所示
class ChatRoomMapAnnotationView: NSObject, MKAnnotation {
let myKeyIdentifier: String
let coordinate: CLLocationCoordinate2D
let title: String?
let subtitle: String?
init(myKeyIdentifier: String, coordinate: CLLocationCoordinate2D, title: String?=nil, subtitle: String?=nil ) {
self.myKeyIdentifier = myKeyIdentifier
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
}
}
self.mapView.addAnnotation(CustomMapAnnotationView(.....))
for annotation in self.chatMapView.annotations {
if let chatAnnotation = annotation as? ChatRoomMapAnnotationView {
if chatAnnotation.myKeyIdentifier == "specialKey" {
// do something special
}
}
}
您需要有用于注释的自定义类:
class CustomPin: MKPointAnnotation {
var yourVar1: yourType1
var yourVar2: yourType2
// ...
var yourVar3: yourType3
}
然后您可以这样使用它:
if annotation is CustomPin {
// do something
}
希望能有帮助