Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GMSGeocoder - how to set response language

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 = @"";
    }
}];
like image 494
Boaz Saragossi Avatar asked Apr 07 '14 09:04

Boaz Saragossi


1 Answers

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 .

like image 185
Levi Han Avatar answered Dec 17 '22 13:12

Levi Han