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

前端 未结 4 791
栀梦
栀梦 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: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.

提交回复
热议问题