我有点难以理解Swift 3中的Apple MapKit。
我在这里找到了一个示例:如何在Swift中使用坐标以编程方式打开地图应用程序?
public func openMapForPlace(lat:Double = 0, long:Double = 0, placeName:String = "") {
let latitude: CLLocationDegrees = lat
let longitude: CLLocationDegrees = long
let regionDistance:CLLocationDistance = 100
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
]
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = placeName
mapItem.openInMaps(launchOptions: options)
}
这个方法完美地运作,但这种情况下我需要使用地址而非坐标。
我找到了使用Google Maps的方法,但是我似乎找不到Apple Maps的特定答案,如果它存在的话,我已经漫不经心地翻过去了。
如果有人能帮我理解正确的方法,那就太棒了。我正在使用:
- Xcode 8.3.1
- Swift 3.1
- macOS
- 目标为 iOS 10+
if let coordinate = placemarks.first?.location?.coordinate
取代if place marks.count != 0
并强制解包所有内容,这是更好的做法。 - EmilioPelaez