GMSGeocoder - how to set response language

北慕城南 提交于 2019-12-08 15:50:16

问题


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?

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 = @"";
    }
}];

回答1:


Just change it to your language parameters

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]));

    }
}



回答2:


Using a GMSGeocoder category can solve this issue, inspired by @DaNLtR After 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

Why should in category?

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.

Why should reset language?

A:Avoide affect other module or framework .



来源:https://stackoverflow.com/questions/22908370/gmsgeocoder-how-to-set-response-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!