ios - 从挂载 map 的数组中检索信息 (MapKit)

标签 ios objective-c iphone swift swift3

我有一个包含多个字典的数组。在每本词典中,我都有一些信息。我想在 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/

相关文章:

ios - NSUserDefaults 显示存储的数据

ios - 在一个属性字符串中使用多种样式

ios - 在动画 block 中调用时,UIView 动画无法无限重复

iphone - Xcode 4 - 使用正则表达式的查找器

iphone - iPhone 中的动态文本字段是否可行?

ios - 解雇ModalViewControllerAnimated时显示正确的UIView :completion: is called

java - 如何检查新的 JSON 文件是否可用?

iphone - 如何在 iPhone SDK 中为 Core Plot 添加图例?

iphone - iPad 模拟器/设备使用 iPhone Storyboard

iphone - iOS 7 和 Helvetica Neue UltraLight : use as default for older iOS versions