反向地理编码以仅以英语返回结果? [英] Reverse geocoding to return results only in English?

查看:14
本文介绍了反向地理编码以仅以英语返回结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码,我请求对当前坐标进行反向地理编码.
一切都很好,除了设备设置为与英语不同的语言.
示例:如果设备的语言是意大利语,而我在意大利,则国家/地区的结果是Italia"而不是Italy".

无论设备的语言如何,如何强制结果仅显示为英语?

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]];
    }
}];

谢谢.

推荐答案

您必须使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆