问题描述
在国外使用我的应用时,Google GMSGeocoder
会自动以本地语言返回响应.如何设置它始终以英语返回响应?
When using my app in a foreign country, the google GMSGeocoder
is returning the response in local language automatically. how can I set it to always return the the response in English?
我正在使用GMS SDK 1.7,我的代码是这样的:
Im using GMS SDK 1.7 and my code is something like this:
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类别可以解决此问题.之后,可以将地址解析器结果设置为英语.
Using a GMSGeocoder category can solve this issue, inspired by @DaNLtRAfter that , It can set geocoder result as English .
@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 reverseGeocodeCoordinate方法之前,它不会影响地址解析器的结果.看到DaNLtR的答案后,我认为我们可以在load方法中设置setLanguge.
A:I tested setLanguage: before GMSGeocoder reverseGeocodeCoordinate method, it can't affect geocoder result. After I saw DaNLtR's answer , I think we can setLanguge in load method.
为什么要重置语言?
A:避免影响其他模块或框架.
A:Avoide affect other module or framework .
这篇关于GMSGeocoder-如何设置响应语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!