反向地理编码仅返回英文结果? [英] Reverse geocoding to return results only in English?
问题描述
使用下面的代码,我要求对当前坐标进行反向地理编码。
除了英语设置为不同的语言外,一切都很好。 b $ b例如:如果设备的语言是意大利语,而我在意大利,则国家的结果是意大利而不是意大利。
我怎样才能不管设备的语言如何,结果都只是英文版本?
Using the code below I'm requesting a reverse geocoding for the current coordinate.
Everything works great, except for when the device is set to a different language than English.
Example: If the language of the device is Italian, and I'm in Italy, the result for country is "Italia" instead of "Italy".
How can I force the results to be only in English, regardless of the language of the device?
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:myCurrentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
[locationController setLastKnownCountry:[placemark country]];
}
}];
谢谢。
Thank you.
推荐答案
你必须使用NSLocale类来管理每种语言的信息。然后,为了您的最终翻译,您必须强制语言环境为英语:
You have to use the NSLocale class that manages info in every language. Then for your final translation you have to force the locale to be in english language:
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:myCurrentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
//Obtains the country code, that is the same whatever language you are working with (US, ES, IT ...)
NSString *countryCode = [placemark ISOcountryCode];
//Obtains a locale identifier. This will handle every language
NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
//Obtains the country name from the locale BUT IN ENGLISH (you can set it as "en_UK" also)
NSString *country = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] displayNameForKey: NSLocaleIdentifier value:identifier];
//Continues your code
[locationController setLastKnownCountry:country];
}
}];
这篇关于反向地理编码仅返回英文结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!