swift - 使用swift中的map函数制作MKPointAnnotations

标签 swift mapkit map-function

我有一个包含坐标和名称的“地点”对象数组。

例如:

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/

相关文章:

perl - 使用映射函数的语法错误

苹果手机 : Custom MKAnnotationView Callouts

javascript - React 中的 Map 函数(err : TypeError: e. map is not a function)

swift - min 和 fmin 之间的区别

arrays - Swift 中具有引用和类型成员的数组相等

swift - 在 Swift 中将 String 转换为 Bool - 通过 API 或大多数类似 Swift 的方法

ios - Swift webview xcode 发布数据

ios - 在注释上显示从当​​前位置到另一点的距离

swift - 设置MKMapView Span而不移动 map

swift - 使用 map 访问数组中的下一个元素