从安装地图的数组中检索信息(MapKit)

我有一个数组字典。 在每个字典中我都有一些信息。 我想在地图上为arrays中的每个项目添加一个点。 我没有任何东西,但没有selectForAnnotation方法,我怎么进入数组内部作为该对象的某些信息? 在表中,我将使用indexPath,但注释没有该indexPath。 有没有解决scheme?

如果你想用你的MKPointAnnotation来设置自定义信息,那么你需要对它进行子类化,并创build一个自定义属性,你可以使用后者来区分它与其他数组对象。

 class MyAnnotation: MKPointAnnotation { var arrayIndex: Int! } 

现在,您需要使用MyAnnotation类创build注释,并在您要创build注释的数组索引中设置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]) } }