xcode 反向地理编码以仅以英文返回结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25144508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reverse geocoding to return results only in English?
提问by thedp
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?
使用下面的代码,我请求对当前坐标进行反向地理编码。
一切正常,除非设备设置为与英语不同的语言。
示例:如果设备的语言是意大利语,而我在意大利,则 country 的结果是“Italia”而不是“Italy”。
无论设备的语言如何,如何强制结果仅显示英文?
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:myCurrentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
[locationController setLastKnownCountry:[placemark country]];
}
}];
Thank you.
谢谢你。
回答by lihudi
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:
您必须使用 NSLocale 类来管理每种语言的信息。然后对于您的最终翻译,您必须强制语言环境为英语:
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];
}
}];
回答by lucasl
It works with the iOS 11 API: reverseGeocodeLocation(_:preferredLocale:completionHandler:)
它适用于 iOS 11 API:reverseGeocodeLocation(_:preferredLocale:completionHandler:)
Sadly I need to support other iOS versions and changing the UserDefaults does neither feel good nor work for me :-(
遗憾的是,我需要支持其他 iOS 版本,而更改 UserDefaults 对我来说既不舒服也不适合我:-(
回答by Raj
You can try the below code to extract the name of the country from the placemark which will show the result in english only regardless the county name
您可以尝试使用以下代码从地标中提取国家/地区名称,该地标将仅以英文显示结果,而不管县名
- (void)findAddress:(CLLocationDegrees)latitude with:(CLLocationDegrees)longitude
{
CLLocation *location =[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Finding address");
if (error) {
NSLog(@"Error %@", error.description);
} else {
NSLog(@"%@",[placemarks valueForKey:@"country"]);
}
}];
}