2

座標と名前を持つ「場所」オブジェクトの配列があります。

例えば:

let places = [place(name: "Eiffel Tower", latitude: 48.8582, longitude: 2.2945), place(name: "Statue of Liberty", latitude: 40.6892, longitude: -74.0444), place(name: "Tower of London", latitude: 51.5081, longitude: -0.0761)]

この配列から、マップを使用して MKPointAnnotations の新しい配列を作成したいと思います。私はそれが次のように始まることを知っています:

let placeAnnotations = places.map{ (place -> MKPointAnnotation // but that's about all I know for sure!}

... 構文エラーを起こさずに MKPointAnnotation の .coordinate および .title プロパティを設定する方法がわかりません。ありがとうございました!

4

1 に答える 1

3

はい、あなたは正しい軌道に乗っています。これは完全なコードです:

let places = [place(name: "Eiffel Tower", latitude: 48.8582, longitude: 2.2945), place(name: "Statue of Liberty", latitude: 40.6892, longitude: -74.0444), place(name: "Tower of London", latitude: 51.5081, longitude: -0.0761)]

let annotations = places.map { aPlace -> MKPointAnnotation in
    let annotation = MKPointAnnotation()
    annotation.coordinate = CLLocationCoordinate2D(latitude: aPlace.latitude, longitude: aPlace.longitude)
    return annotation
}

println(annotations)
于 2015-01-09T16:24:43.347 に答える