Ios 从装载地图的阵列(MapKit)中检索信息
我有一个包含几个字典的数组。在每本字典里我都有一些信息。我想在地图上为数组中的每个项目添加一个点。除了didSelectForAnnotation方法,我什么都没有,如何在数组中获取该对象的特定信息?在表中,我将使用indexPath,但注释没有该indexPath。是否有解决方案???如果要使用Ios 从装载地图的阵列(MapKit)中检索信息,ios,objective-c,iphone,swift,swift3,Ios,Objective C,Iphone,Swift,Swift3,我有一个包含几个字典的数组。在每本字典里我都有一些信息。我想在地图上为数组中的每个项目添加一个点。除了didSelectForAnnotation方法,我什么都没有,如何在数组中获取该对象的特定信息?在表中,我将使用indexPath,但注释没有该indexPath。是否有解决方案???如果要使用MKPointAnnotation设置自定义信息,则需要对其进行子类化,并创建自定义属性,以便使用后者将其与其他数组对象区分开来 class MyAnnotation: MKPointAnnotatio
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)
}
现在,在地图视图的didSelect view
方法中,您可以获得该索引
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let annotation = view.annotation as? MyAnnotation {
print(annotation.arrayIndex)
print(yourArray[annotation.arrayIndex])
}
}
非常感谢。我将尝试此操作,稍后再回答。我收到此错误:“无法将类型为'nskvontifiing_MKUserLocation'(0x7c985000)的值强制转换为'Floop.MyAnnotation'”