Reputation: 2773
How does one convert a zip, or in my case a postal code(UK) to lat long coordinates?
I read upon using google geocoding api but read that is has a fair amount of restrictions such as using google maps in the application and limit to requests.
I also found this but I'm not to sure how to go about implementing it
Upvotes: 5
Views: 2066
Reputation: 2818
You would want to use the GeocodeAddressDictionary which accepts a dictionary of specific address types.
In your case, you would do the following.
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
}
}
Upvotes: 6
Reputation: 4734
With this code you can do it providing the address (Will be more accurate because a zip code is not a concrete coordinate but an address it is).
Remember to import CoreLocation
at the top of the class.
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
}
})
Upvotes: 2