Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLLocationManager reverseGeocodeLocation Language

I'm using CLLocationManager on iPhone to get the current user location, then I use reverseGeocodeLocation to convert the location to an address.

This works fine but the results are returned in the device language and I need them to always be in english.

Is there a way to get the Address string in a desired language or convert it ?

    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
    }
like image 688
Shachar Avatar asked Dec 05 '13 00:12

Shachar


2 Answers

I managed to figure it out eventually so this is for those who might need it as well.

  1. Get the "AppleLanguages" key from SUserDefaults standardUserDefaults and save it for later.
  2. Set the "AppleLanguages key to only "en"
  3. Grab the location (Now it's only in english)
  4. Restore the "AppleLanguages" key from before.

//1+2

NSArray *currentLanguageArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];
         //get the location

//4

[[NSUserDefaults standardUserDefaults] setObject:currentLanguageArray forKey:@"AppleLanguages"];

Hope that helps

like image 121
Shachar Avatar answered Nov 05 '22 06:11

Shachar


CLGeocoder provides a handy method for this exact case:

- (void)reverseGeocodeLocation:(CLLocation *)location 
               preferredLocale:(NSLocale *)locale 
             completionHandler:(CLGeocodeCompletionHandler)completionHandler;

Available in iOS 11 and above.

like image 2
Vladimir Grigorov Avatar answered Nov 05 '22 07:11

Vladimir Grigorov