如何将邮政编码(或者在我的情况下是邮政编码(英国))转换为经纬度坐标?
我读到了使用谷歌地理编码 API,但读到它有相当多的限制,例如在应用程序中使用谷歌地图和限制请求。
我也发现了这个,但我不确定如何实施它
您可能需要使用接受特定地址类型字典的 GeocodeAddressDictionary。
对于您的情况,您将执行以下操作。
let geocoder = CLGeocoder()
let dic = [NSTextCheckingZIPKey: yourZipCode]
geocoder.geocodeAddressDictionary(dic) { (placemark, error) in
if((error) != nil){
print(error!)
}
if let placemark = placemark?.first {
let lat = placemark.location!.coordinate.latitude
let long = placemark.location!.coordinate.longitude
}
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,您可以提供地址(会更准确,因为邮政编码不是具体坐标,而是地址)。
记住要import CoreLocation
在班级中名列前茅。
let address = "1 Infinite Loop, CA, USA"
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error)
}
if let placemark = placemarks?.first {
let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
}
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1925 次 |
最近记录: |