2015-01-09 158 views
2

我有一個包含座標和名稱的「地點」對象數組。在swift中使用map函數使MKPointAnnotations

例如:

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僞屬性。謝謝!

回答

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)] 

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

println(annotations) 
+0

正是我在找什麼 - 非常感謝。對不起,我沒有提高答案的聲望。 –

+0

我很高興這是對你的幫助:)沒關係,我不是在這裏的代表;) – HAS

相關問題