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

geocodeaddressstring not working
clplacemark init
clplacemark postaladdress
get address from latitude and longitude objective-c
mkplacemark vs clplacemark
placemark from coordinates
reversegeocodelocation swift
geocoder xcode

I uses CLGeocoder to decode the CLLocation from longitude/latitude to place names. It works fine. But there is still one thing bothers me. 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];

}

is displayed in english, like : chengdu.

when i change the device language to Chinese,

placemarker.locality

returns a Chinese character value.

But what i really want is that it will always return an English character value (no Chinese character value). I guess this is something related to the locale. Can anyone help on this? Thanks.

Usually, it is not a good practice to mess with user locales. If the device language is set to Chinese is because the user want to read Chinese characters so, why do you want to show him in English when he already told you that he want Chinese?

Anyway, if for any reason you need to force english, you can trick the geoCoder which uses the standardUserDefaults first language so you can do something like this just before calling the geoCoder method:

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

This way, geoCoder will give you all the information in english.

However, this will change the user preferences so it is a best approach to give 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 should really think why you need this, but if you really need this, that would work.

CLPlacemark returns all informatio…, Usually, it is not a good practice to mess with user locales. If the device language is set to Chinese is because the user want to read Chinese characters so, why  var locality: String? { get} Discussion If the placemark location is Apple’s headquarters, for example, the value for this property would be the string “Cupertino”.

I found a nice solution

NSString *country = placemark.ISOcountryCode;

This will return the exact country no matter your locale is. For example country will be @"US" instead of @"United States"

CLPlacemark, CLPlacemark returns all information except thoroughfare Subthoroughfare returns a value when one is available which I would expect to sometimes be nil. I get the same results regardless of simulator or on device. name: Optional("I-​280 N"), thoroughfare: nil, subthoroughfare: nil, locality: Optional("Cupertino"), country:  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

From ios 11 you can pass a preferredLocale parameter to geocoder reverseGeocodeLocation method.

In Swift:

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

A preferredLocale value example:

Locale(identifier: "en_US")

Using native Apple's Places - Flawless iOS, A user-friendly description of a geographic coordinate, often containing the name of the place, its address, and other relevant information. Availability. iOS 5.0+  Your app's default language is en_US (US English), and it also has Spanish strings localized in es_ES resource files. A device is set to es_MX When your Java code refers to strings, the system would load strings from the default ( en_US ) resource file, even if the app has Spanish resources localized under es_ES .

🤳 ‍ https://translate.googleusercontent.com/translate_c?depth , CLPlacemark.locality, value changes if the device language is different. I uses CLGeocoder to decode the CLLocation from longitude/latitude to place names. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

xcode, Guide how to use CLPlacemark — a user-friendly description of a geographic coordinate, often The system language of the device. Example  Changing the language on Android at runtime was never officially encouraged or documented. The resource framework automatically selects the resources that best match the device. Such behavior is enough for common applications, so just make sure you have strict reasons to change it before proceeding further.

how to get plus code by using latitude and longitude, variables - windows · CLPlacemark.locality, changing the value if the device language is different - ios · Sort characters in NSString alphabetically - objective-​c  Medicare PFS Locality Configuration The current Physician Fee Schedule (PFS) locality structure was developed and implemented in 1997. There are currently 89 total PFS localities; 34 localities are statewide areas (that is, only one locality for the entire state).

Comments
  • Tried this solution but it doesn't work on iOS 9 or is that because of simulator. Let me know if anyone knows about any workaround.
  • it doesn't work for me too.. @MohammadAshrafulKabir do you have any update on this?
  • @MohammadAshrafulKabir Setting back initial language: [[NSUserDefaults standardUserDefaults] setObject:userDefaultLanguages forKey:@"AppleLanguages"] should be placed under completionHandler block, in this case the solution works fine