在Swift 3中,我有点难以理解苹果的MapKit。
我在这里找到了一个例子:How to open maps App programmatically with coordinates in 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)
}这是绝对顺利的工作,除了我需要使用一个地址,而不是坐标在这种情况下。
我已经找到了用谷歌地图做这件事的方法,但是我似乎找不到苹果地图的具体答案,如果它存在的话,我已经把它上了釉。
如果有人能帮我理解正确的方法,那就太棒了。我在用:
发布于 2017-05-11 14:59:11
您需要使用Geocoding服务将地址转换为相应的地理位置。
例如,将此函数添加到工具箱中:
func coordinates(forAddress address: String, completion: @escaping (CLLocationCoordinate2D?) -> Void) {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) {
(placemarks, error) in
guard error == nil else {
print("Geocoding error: \(error!)")
completion(nil)
return
}
completion(placemarks.first?.location?.coordinate)
}
}然后像这样使用它:
coordinates(forAddress: "YOUR ADDRESS") {
(location) in
guard let location = location else {
// Handle error here.
return
}
openMapForPlace(lat: location.latitude, long: location.longitude)
}发布于 2017-05-11 14:57:59
你需要用geoCode从地址中得到坐标.这应该是可行的:
let geocoder = CLGeocoder()
geocoder.geocodeAddressString("ADDRESS_STRING") { (placemarks, error) in
if error != nil {
//Deal with error here
} else if let placemarks = placemarks {
if let coordinate = placemarks.first?.location?.coordinate {
//Here's your coordinate
}
}
}https://stackoverflow.com/questions/43918842
复制相似问题