使用下面的代码,我要求为当前坐标请求反向地理编码
除了将设备设置为与英语不同的语言外,一切都很有效
示例:如果设备的语言是意大利语,而我在意大利,那么国家的结果是" Italia"而不是"意大利"。
无论设备的语言如何,我如何强制结果只能用英语?
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:myCurrentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
[locationController setLastKnownCountry:[placemark country]];
}
}];
谢谢。
答案 0 :(得分:18)
您必须使用管理每种语言信息的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];
}
}];
答案 1 :(得分:3)
适用于iOS 11 API:reverseGeocodeLocation(_:preferredLocale:completionHandler:)
可悲的是,我需要支持其他iOS版本,并且更改UserDefaults既不会感觉良好也不适合我: - (
答案 2 :(得分:-4)
您可以尝试使用以下代码从地标中提取国家/地区的名称,该地标只会显示英文结果,而不管县名
- (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"]);
}
}];
}