我想在自己的Swift App中打开Apple Maps App,但是我只有邮政编码,城市和街道。我没有坐标。我做了很多研究,但是只有使用协调信息的方法。
最佳答案
您只需在打开 map 应用的URL中将地址信息作为URL参数传递即可。假设您要以白宫为中心打开 map 应用。
UIApplication.sharedApplication().openURL(NSURL(string: "http://maps.apple.com/?address=1600,PennsylvaniaAve.,20500")!)
将打开“ map ”应用,在搜索字段中显示丑陋的查询字符串,但显示正确的位置。请注意,搜索查询中没有城市和州,只是街道地址和邮政编码。
根据您的需求,一种可能更好的方法是使用CLGeocoder获取您拥有的地址信息的CLLocation。
let geocoder = CLGeocoder()
let str = "1600 Pennsylvania Ave. 20500" // A string of the address info you already have
geocoder.geocodeAddressString(str) { (placemarksOptional, error) -> Void in
if let placemarks = placemarksOptional {
print("placemark| \(placemarks.first)")
if let location = placemarks.first?.location {
let query = "?ll=\(location.coordinate.latitude),\(location.coordinate.longitude)"
let path = "http://maps.apple.com/" + query
if let url = NSURL(string: path) {
UIApplication.sharedApplication().openURL(url)
} else {
// Could not construct url. Handle error.
}
} else {
// Could not get a location from the geocode request. Handle error.
}
} else {
// Didn't get any placemarks. Handle error.
}
}