我有一个"地点"具有坐标和名称的对象。
例如:
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)]
从这个数组中,我想使用map创建一个新的MKPointAnnotations数组。我知道它是这样开始的:
let placeAnnotations = places.map{ (place -> MKPointAnnotation // but that's about all I know for sure!}
...我不知道如何在不提交语法错误的情况下设置MKPointAnnotation的.coordinate和.title属性。谢谢!
答案 0 :(得分: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)