2017-02-09 5 views
0

私はいくつかの辞書を持つ配列を持っています。すべての辞書にはいくつかの情報があります。私は配列内の各項目のためにマップ上にポイントを追加したいと思います。私はdidSelectForAnnotationメソッドを何も持っていない、どのようにそのオブジェクトの特定の情報として配列の内部に入るのですか?テーブルでは、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

関連する問題