2015-01-09 160 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

相关问题