从安装地图的数组(MapKit)中检索信息 [英] Retrieving information from the array that mounted the map (MapKit)

查看:59
本文介绍了从安装地图的数组(MapKit)中检索信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有几个字典的数组.在每本词典中,我都有一些信息.我想在地图上为数组内的每个项目添加一个点.除了didSelectForAnnotation方法之外,我什么都没有,我如何作为对象的某些信息进入数组?在表中,我将使用indexPath,但注释中没有该indexPath.有解决办法吗?

I have an array with several dictionaries. In every dictionary I have some information. I want to add a point on the map for each item inside the array. I do not have anything but didSelectForAnnotation method, how do I get inside the array as certain information of that object? In a table I would use the indexPath but an annotation does not have that indexPath. Is there a solution??

推荐答案

如果要与您一起设置 MKPointAnnotation 的自定义信息,则需要对其进行子类化并创建一个自定义属性,以供稍后使用区别于其他数组对象.

If you want to set custom information with you MKPointAnnotation then you need to subclass it and create a custom property that you can use latter to differentiate it from other array object.

class MyAnnotation: MKPointAnnotation {

     var arrayIndex: Int!
}

现在,您需要使用 MyAnnotation 类创建注释,并使用要创建注释的数组索引设置arrayIndex.

Now you need to create annotation with MyAnnotation class, and set the arrayIndex with your array index where you are creating annotation.

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视图方法中,您可以获取该索引.

Now in didSelect view method of mapView you can get that index.

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if let annotation = view.annotation as? MyAnnotation {
        print(annotation.arrayIndex)
        print(yourArray[annotation.arrayIndex])
    } 
}

这篇关于从安装地图的数组(MapKit)中检索信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆