我有一个包含坐标和名称的“地点”对象数组。
例如:
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 属性而不犯一些语法错误。谢谢!
最佳答案
是的,您走在正确的道路上。这是完整的代码:
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)
关于swift - 使用swift中的map函数制作MKPointAnnotations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27864761/