GMSGeocoder - how to set response language
Asked Answered
M

2

10

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 = @"";
    }
}];
Muskogee answered 7/4, 2014 at 9:23 Comment(3)
I am currently looking for the same answer. Are you able to solve this problem?Youlandayoulton
Haven't found a solution yet, i'm currently using the apple CLGeocoder service instead. (But i find that its less accurate)Muskogee
hm... ok. Thanks for letting me know. I will try apple CLGeocoder as well and see how it goes.Youlandayoulton
R
1

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 .

Rosinweed answered 25/6, 2018 at 3:3 Comment(1)
Don't use -[NSUserDefaults synchronize]. From Apple's documentation "this method is unnecessary and shouldn't be used."Brucebrucellosis
J
0

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

    }
}
Jeramie answered 29/4, 2018 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.