【发布时间】:2015-07-20 05:42:52
【问题描述】:
我有一个使用 MKAnnotations 填充标记的 mapView。我能够很好地获得注释数组。但是,我如何找出被点击的标记的索引?假设我点击了一个标记并弹出了 MKAnnotation。如何获取此注解实例?
ViewDidAppear 代码:
for (var i=0; i<latArray.count; i++) {
let individualAnnotation = Annotations(title: addressArray[i],
coordinate: CLLocationCoordinate2D(latitude: latArray[i], longitude: longArray[i]))
mapView.addAnnotation(individualAnnotation)
}
//store annotations into a variable
var annotationArray = self.mapView.annotations
//prints out an current annotations in array
//Result: [<AppName.Annotations: 0x185535a0>, <AppName.Annotations: 0x1663a5c0>, <AppName.Annotations: 0x18575fa0>, <AppName.Annotations: 0x185533a0>, <AppName.Annotations: 0x18553800>]
println(annotationArray)
注解类: 导入 MapKit
class Annotations: NSObject, MKAnnotation {
let title: String
//let locationName: String
//let discipline: String
let coordinate: CLLocationCoordinate2D
init(title: String, coordinate: CLLocationCoordinate2D) {
self.title = title
self.coordinate = coordinate
super.init()
}
var subtitle: String {
return title
}
}
【问题讨论】: