xcode 如何本地化来自 reverseGeocodeLocation 的地址结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8809555/
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
how to localize address result from reverseGeocodeLocation?
提问by user513790
My iphone app supposed to resolve address based on latitude and longitude of the user. reverseGeocodeLocation works fine, but results are in english.
我的 iphone 应用程序应该根据用户的纬度和经度解析地址。reverseGeocodeLocation 工作正常,但结果是英文的。
Is there a way to localize the results to other languages?
有没有办法将结果本地化为其他语言?
couldnt find any information about it at apple or anywhere else.
在苹果或其他任何地方都找不到有关它的任何信息。
The code I use is:
我使用的代码是:
CLGeocoder *geocoder = [[[CLGeocoder alloc] init] autorelease];
CLLocation *location = [[[CLLocation alloc]
initWithLatitude:coord.latitude longitude:coord.longitude] autorelease];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!");
if (error){
NSLog(@"Geocode failed with error: %@", error);
[self displayError:error];
return;
}
if(placemarks && placemarks.count > 0)
{
//do something
CLPlacemark *topResult = [placemarks objectAtIndex:0];
NSString *addressTxt = [NSString stringWithFormat:@"%@ %@,%@ %@",
[topResult subThoroughfare],[topResult thoroughfare],
[topResult locality], [topResult administrativeArea]];
}
}
回答by rozochkin
i found how to localize country name, maybe it'll help:
我找到了如何本地化国家/地区名称,也许它会有所帮助:
CLPlacemark *placemark;
NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: placemark.ISOcountryCode forKey: NSLocaleCountryCode]];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSString *country = [usLocale displayNameForKey: NSLocaleIdentifier value: identifier];
insert any country id instead @"en_US"
插入任何国家 ID 而不是 @"en_US"
回答by deevee
Solution for Swift 4, iOS 11
适用于Swift 4、iOS 11 的解决方案
You can force to get geocoding results in chosen locale language by setting argument: preferredLocale: Locale.init(identifier: "en_US")
.
您可以通过设置参数强制获取所选区域设置语言的地理编码结果:preferredLocale: Locale.init(identifier: "en_US")
。
CLGeocoder().reverseGeocodeLocation(location, preferredLocale: Locale.init(identifier: "en_US"), completionHandler: {(placemarks, error) -> Void in
print(location)
if error != nil {
print("Reverse geocoder failed with error" + error!.localizedDescription)
return
}
if placemarks!.count > 0 {
let pm = placemarks![0]
print(pm.administrativeArea!, pm.locality!)
}
else {
print("Problem with the data received from geocoder")
}
})
回答by Derrick Hathaway
Mattt Thompsonhas a great writeupon using a method from the AddressBookUI.framework for localizing addresses on his site NSHipster. He also has a formatting libraryon github which contains a class for doing this type of localization which uses the AddressBookUI.framework approach he has described.
Mattt Thompson有一篇关于使用 AddressBookUI.framework 中的方法在他的站点NSHipster上本地化地址的精彩文章。他还在github 上有一个格式库,其中包含一个用于执行此类本地化的类,该类使用他所描述的 AddressBookUI.framework 方法。
回答by tounaobun
Since iOS11, apple provides us with another API which can set locale to print localized language.
从iOS11开始,苹果为我们提供了另一个API,可以设置locale来打印本地化语言。
- (void)reverseGeocodeLocation:(CLLocation *)location preferredLocale:(nullable NSLocale *)locale
completionHandler:(CLGeocodeCompletionHandler)completionHandler API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));