在国外使用我的应用程序时,谷歌GMSGeocoder
自动以本地语言返回响应。我怎样才能将其设置为始终返回英文响应?
我使用 GMS SDK 1.7,我的代码是这样的:
GMSGeocoder *geoCoder = [[GMSGeocoder alloc] init];
[geoCoder reverseGeocodeCoordinate:self.cellLocation.coordinate completionHandler:^(GMSReverseGeocodeResponse *respones, NSError *err) {
if([respones firstResult]) {
GMSAddress* address = [respones firstResult];
NSString* fullAddress = [NSString stringWithFormat:@"%@, %@",address.thoroughfare, address.locality];
self.theTextField.text = fullAddress;
} else {
self.theTextField.text = @"";
}
}];
受到 @DaNLtR 的启发,使用 GMSGeocoder 类别可以解决这个问题
之后,它可以将地理编码结果设置为英文。
@implementation GMSGeocoder (Load)
+(void)load {
[[self class] setUserLanguage:@"en-CN"];// set your wanted language.
NSLog(@"GMSGeocoder + load!");
}
- (void)dealloc {
[[self class] resetSystemLanguage];
NSLog(@"GMSGeocoder + dealloc!");
}
+ (void)setUserLanguage:(NSString *)userLanguage
{
if (!userLanguage.length) {
[[self class] resetSystemLanguage];
return;
}
[[NSUserDefaults standardUserDefaults] setValue:userLanguage forKey:@"UserLanguage"];
[[NSUserDefaults standardUserDefaults] setValue:@[userLanguage] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+ (void)resetSystemLanguage
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"UserLanguage"];
[[NSUserDefaults standardUserDefaults] setValue:nil forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
@end
为什么要在类别中?
A:我测试了setLanguage:在GMSGeocoder的reverseGeocodeCooperative方法之前,它不会影响地理编码器的结果。在我看到DaNLtR的回答后,我认为我们可以在load方法中setLanguge。
为什么要重置语言?
A:避免影响其他模块或框架。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)