CLPlacemark.locality, changing the value if the device language is different

I use CLGeocoder to decode CLLocation from longitude / latitude to place names. It is working fine. But I'm still worried. When I set the device language to English, the result of the code below:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ /* We received the new location */ NSLog(@"Latitude = %f", newLocation.coordinate.latitude); NSLog(@"Longitude = %f", newLocation.coordinate.longitude); [self.geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray* placemarks, NSError* error){ MKPlacemark *placemarker = [placemarks objectAtIndex:0]; NSLog(@"%@",placemarker.locality); }]; [self.locationManager stopUpdatingLocation]; 

}

displayed in English, for example: chengdu.

when I change the device language to Chinese,

 placemarker.locality 

returns the value of the Chinese character.

But I really want it to always return the value of the English character (without the value of the Chinese character). I assume this is due to the locale. Can anyone help with this? Thanks.

+7
source share
3 answers

This is generally not a good practice for communicating with local users. If the device language is set to Chinese, it is that the user wants to read Chinese characters, why do you want to show it in English when he already told you that he wants a Chinese?

In any case, if for some reason you need to force English, you can trick geoCoder, which uses the standard language standardUserDefaults, so that you can do something like this immediately before calling the geoCoder method:

 [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"]; 

In this way, geoCoder will provide you with all the information in English.

However, this will change the user settings, so this is the best approach to get them back to where they were:

 NSMutableArray *userDefaultLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"]; [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"]; [self.geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray* placemarks, NSError* error){ MKPlacemark *placemarker = [placemarks objectAtIndex:0]; NSLog(@"%@",placemarker.locality); }]; [[NSUserDefaults standardUserDefaults] setObject:userDefaultLanguages forKey:@"AppleLanguages"]; 

As I said, you really need to think about why you need it, but if you really need it, it will work.

+14
source

I found a nice solution

 NSString *country = placemark.ISOcountryCode; 

This will return the exact country regardless of your locale. For example, the country would be @ "USA" instead of @ "USA"

+5
source

Starting with ios 11, you can pass the preferred object parameter to the reverseGeocodeLocation geocoder method.

In Swift:

 geocoder.reverseGeocodeLocation( location: CLLocation, preferredLocale: Locale?, completionHandler: {} ) 

An example of a preferred local code value:

 Locale(identifier: "en_US") 
0
source

All Articles