如何将zip文件,或者在我这种情况下是英国的邮政编码转换成经纬度坐标?
我了解到可以使用Google地理编码API进行转换,但是它有很多限制,比如必须在应用中使用Google地图,并且有请求次数的限制。
我还发现了这个,但我不太确定如何实施。
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
}
}
使用这段代码,您可以提供地址来完成操作(因为邮政编码不是具体的坐标,而是地址)。
请记得在类的顶部导入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
}
})