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])
}
}
ありがとうございます。私はこれを試し、後で私は答えます。 – breno
@brenoそれは今働いていますか? –
私はこのエラーを受け取ります: "タイプ 'NSKVONotifying_MKUserLocation'(0x7c985000)の値を 'Floop.MyAnnotation'にキャストできませんでした。" – breno