How to force language in which reverse geocoding data on iOS is received?

前端 未结 4 788
栀梦
栀梦 2021-01-14 06:04

When i call

geocoder reverseGeocodeLocation:currentLoc completionHandler:

i get all the data (city, county, ...) in a language according t

相关标签:
4条回答
  • 2021-01-14 06:19

    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

    0 讨论(0)
  • 2021-01-14 06:26

    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
    }];
    
    0 讨论(0)
  • 2021-01-14 06:34

    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.

    0 讨论(0)
  • 2021-01-14 06:34

    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.

    0 讨论(0)
提交回复
热议问题