How to force language in which reverse geocoding d

2019-02-18 17:43发布

问题:

When i call

geocoder reverseGeocodeLocation:currentLoc completionHandler:

i get all the data (city, county, ...) in a language according to locale set on iphone.

How can i force to always get this data in English?

回答1:

You can not force the language of the geo data retrieved from CLGeocoder or MKReverseGeocoder. It will always be the system language of the device. If you want to get the data in English you need to built your own geocoder which is relatively easy to implement with the Google Maps API.

Here is a spreadsheet of the supported languages in Google Maps API: https://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1



回答2:

For future readers, the accepted answer is not true (at least not any more).

UserDefaults.standard.set(["en"], forKey: "AppleLanguages")
gc.reverseGeocodeLocation(loc) {
    print($0 ?? $1!)

    UserDefaults.standard.removeObject(forKey: "AppleLanguages")
    print(UserDefaults.standard.object(forKey: "AppleLanguages") as! [String])
}

Calling removeObject(forKey:) is important since you want to reset the returned value in UserDefaults to the system settings. Some answers make you hold the original value from calling UserDefaults.standard.object(forKey: "AppleLanguages") and set it after getting the address, but this will stop your UserDefaults from being in sync with the global Language Preferences in iOS "Settings" app.



回答3:

iOS 11 has a new -reverseGeocode... method that accepts a locale to use:

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

Swift signature:

func reverseGeocodeLocation(_ location: CLLocation, preferredLocale locale: Locale?, completionHandler: @escaping CLGeocodeCompletionHandler)

Put whatever locale you like. This example just uses the current locale

NSLocale *currentLocale = [NSLocale currentLocale];
[self.geocoder reverseGeocodeLocation:self.location preferredLocale:currentLocale
    completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

    // Handle the result or error
}];


回答4:

Update Swift 4:

Just use this code to force the data returned in English:

func fetchCityAndCountry(from location: CLLocation, completion: @escaping (_ locality: String?, _ country:  String?, _ error: Error?) -> ()) {
    CLGeocoder().reverseGeocodeLocation(location, preferredLocale: Locale.init(identifier: "en")) { placemarks, error in
        completion(placemarks?.first?.locality,
                   placemarks?.first?.country,
                   error)
    }
}

You can change the Locale identifier to translate the data into any other language ("ca", "es", "fr", "de"...).

This function can be called, for instance, like this:

fetchCityAndCountry (from: userLocationCL) { locality, country, error in
   guard let locality = locality, let country = country, error == nil else { return }
   // your code
}

Where userLocationCL is the current user location.