我有一个包含多个字典的数组。在每本词典中,我都有一些信息。我想在 map 上为数组中的每个项目添加一个点。除了 didSelectForAnnotation 方法,我什么都没有,我如何进入数组作为该对象的某些信息?在表中,我会使用 indexPath,但注释没有该 indexPath。有解决办法吗??
最佳答案
如果您想使用 MKPointAnnotation
设置自定义信息,那么您需要对其进行子类化并创建一个自定义属性,您可以使用后者将其与其他数组对象区分开来。
class MyAnnotation: MKPointAnnotation {
var arrayIndex: Int!
}
现在您需要使用 MyAnnotation
类创建注释,并使用您创建注释的数组索引设置 arrayIndex。
for (index,item) in yourArray.enumerated() {
let annotation = MyAnnotation()
//Set arrayIndex for your annotation
annotation.arrayIndex = 1
//Set coordinate and title from your array
annotation.coordinate = locations
annotation.title = "Zaid Homes"
annotation.subtitle = "Hay aljameaa"
map.addAnnotation(annotation)
}
现在在 mapView 的 didSelect view
方法中,您可以获得该索引。
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let annotation = view.annotation as? MyAnnotation {
print(annotation.arrayIndex)
print(yourArray[annotation.arrayIndex])
}
}
关于ios - 从挂载 map 的数组中检索信息 (MapKit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42135662/