2017-02-09 76 views
0

我有一個包含多個字典的數組。在每個字典中我都有一些信息。我想在地圖上爲陣列中的每個項目添加一個點。我沒有任何東西,但沒有選擇ForAnnotation方法,我該如何進入數組內作爲該對象的某些信息?在表中,我會使用indexPath,但註釋沒有該indexPath。有沒有解決方案?從安裝地圖的數組中檢索信息(MapKit)

回答

1

如果您想要與您設置自定義信息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方法的mapView中,你可以得到該索引。

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

謝謝。我會試試這個,稍後我會回答。 – breno

+0

@breno現在工作嗎? –

+0

我收到此錯誤:「無法將類型'NSKVONotifying_MKUserLocation'(0x7c985000)的值轉換爲'Floop.MyAnnotation'」 – breno