Reputation:
When a user taps a particular place on a MapKit
map, I need to know if a country is a particular country (eg. Spain), then I must perform actions based on the retrieved information.
I initially thought to use the country name I get from:
reverseGeocodeLocation(_ location: CLLocation,
completionHandler: @escaping CLGeocodeCompletionHandler)
but the country name is strictly related to the device locale settings, so it's not really a good idea use the country name...
Then, I thought getting the isoCountryCode
would have been a better choice, but, I don't know why in my tests CLPlacemark
doesn't return it.
Finally, there's another version of the CLGeocoder().reverseGeocodeLocation
function, that lets you specify a preferredLocale
. Is this the way to go?
func reverseGeocodeLocation(_ location: CLLocation,
preferredLocale locale: Locale?,
completionHandler: @escaping CLGeocodeCompletionHandler)
Upvotes: 1
Views: 227
Reputation: 2189
To get address info from lat & long,
Swift
let loc: CLLocation = CLLocation(latitude:center.latitude, longitude: center.longitude)
geoCoder.reverseGeocodeLocation(loc, completionHandler:
{(placemarks, error) in
if (error != nil)
{
print("reverse geodcode fail: \(error!.localizedDescription)")
}
let pm = placemarks! as [CLPlacemark]
if pm.count > 0 {
let pm = placemarks![0]
print(pm.country)
print(pm.ISOcountryCode)
}
})
}
Obj-C
CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
__block CLPlacemark *placemark = nil;
[geocoder reverseGeocodeLocation:currentLocation
completionHandler:^(NSArray *placemarks, NSError *error)
{
if (error){
NSLog(@"************************************* Geocode failed with error:************************************* \n %@", error);
}else{
placemark = [placemarks objectAtIndex:0];
NSLog(@"Current placemark %@", placemark);
NSLog(@"Current country code: %@", [placemark ISOcountryCode]);
}
}];
Upvotes: 1