10

在国外使用我的应用程序时,谷歌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 = @"";
    }
}];
4

2 回答 2

0

使用 GMSGeocoder 类别可以解决这个问题,灵感来自 @DaNLtR 之后,它可以将 geocoder result 设置为 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 方法之前,它不会影响geocoder 结果。在我看到 DaNLtR 的回答后,我想我们可以在 load 方法中设置语言。

为什么要重设语言?

A:避免影响其他模块或框架。

于 2018-06-25T03:03:54.000 回答
0

只需将其更改为您的语言参数

int main(int argc, char * argv[])
{
    @autoreleasepool {

        //For Google Maps hebrew response
        //[[NSUserDefaults standardUserDefaults] setObject:@[@"en"] forKey:@"AppleLanguages"];
        [[NSUserDefaults standardUserDefaults] setObject:@[@"he",@"he-IL"] forKey:@"AppleLanguages"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

    }
}
于 2018-04-29T07:57:30.383 回答