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

从这个数组中,我想使用 map 创建一个新的 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 回答