Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse geocoding to return results only in English?

Using the code below I'm requesting a reverse geocoding for the current coordinate.
Everything works great, except for when the device is set to a different language than English.
Example: If the language of the device is Italian, and I'm in Italy, the result for country is "Italia" instead of "Italy".

How can I force the results to be only in English, regardless of the language of the device?

CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:myCurrentLocation completionHandler:^(NSArray *placemarks, NSError *error) {

    for (CLPlacemark * placemark in placemarks) {

        [locationController setLastKnownCountry:[placemark country]];
    }
}];

Thank you.

like image 856
thedp Avatar asked Aug 05 '14 17:08

thedp


Video Answer


1 Answers

You have to use the NSLocale class that manages info in every language. Then for your final translation you have to force the locale to be in english language:

CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:myCurrentLocation completionHandler:^(NSArray *placemarks, NSError *error) {

    for (CLPlacemark * placemark in placemarks) {
         //Obtains the country code, that is the same whatever language you are working with (US, ES, IT ...)
         NSString *countryCode = [placemark ISOcountryCode];
         //Obtains a locale identifier. This will handle every language
         NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
         //Obtains the country name from the locale BUT IN ENGLISH (you can set it as "en_UK" also)
         NSString *country = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] displayNameForKey: NSLocaleIdentifier value:identifier];

        //Continues your code
        [locationController setLastKnownCountry:country];
    }
}];
like image 71
lihudi Avatar answered Sep 18 '22 21:09

lihudi