iOS Force geolocation to return city name in a specific language

风格不统一 提交于 2019-12-10 10:35:57

问题


I've encountered a rather strange situation where the backend developer asked me to force the city name that is returned from performing a geolocation to be in a specific language? For instance have it return København instead of Copenhagen no matter what language the iOS is to. I couldn't find anything on the web regarding this. Your help will be much appreciated. Thanks


回答1:


Not sure if this is the best and safest solution but you could temporarily change the locale while you make the reverse geo code call and then reset it when you are done.

NSArray *defaultLanguages = [[NSUserDefaults standardUserDefaults] valueForKey:@"AppleLanguages"];
NSArray *languages = [NSArray arrayWithObject:@"da"];
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = placemarks[0];
        NSString *danish = placemark.country;
        [[NSUserDefaults standardUserDefaults] setObject:defaultLanguages forKey:@"AppleLanguages"];
        [[NSUserDefaults standardUserDefaults] synchronize];
}];


来源:https://stackoverflow.com/questions/18310237/ios-force-geolocation-to-return-city-name-in-a-specific-language

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