CLPlacemark.locality,如果设备语言不同,则值会更改 [英] CLPlacemark.locality, value changes if the device language is different

查看:525
本文介绍了CLPlacemark.locality,如果设备语言不同,则值会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CLGeocoder将CLLocation从经度/纬度解码为地名。它工作正常。但是仍然有一件事困扰着我。当我将设备语言设置为英语时,代码的结果如下:

I uses CLGeocoder to decode the CLLocation from longitude/latitude to place names. It works fine. But there is still one thing bothers me. When i set the device language to English, the result of the code below:

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation{
       /* We received the new location */
       NSLog(@"Latitude = %f", newLocation.coordinate.latitude);
       NSLog(@"Longitude = %f", newLocation.coordinate.longitude);
       [self.geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray* placemarks, NSError* error){
           MKPlacemark *placemarker = [placemarks objectAtIndex:0];
           NSLog(@"%@",placemarker.locality);
       }];
      [self.locationManager stopUpdatingLocation];

}

以英文显示,喜欢:成都。

is displayed in english, like : chengdu.

当我将设备语言改为中文时,

when i change the device language to Chinese,

placemarker.locality

返回中文字符值。

但我真正想要的是它总会返回一个英文字符值(没有中文字符值)。
我想这是与语言环境有关的东西。任何人都可以帮忙吗?谢谢。

But what i really want is that it will always return an English character value (no Chinese character value). I guess this is something related to the locale. Can anyone help on this? Thanks.

推荐答案

通常,混淆用户区域设置不是一个好习惯。如果将设备语言设置为中文是因为用户想要阅读中文字符,为什么当他已经告诉你他想要中文时,你想用英语给他看?

Usually, it is not a good practice to mess with user locales. If the device language is set to Chinese is because the user want to read Chinese characters so, why do you want to show him in English when he already told you that he want Chinese?

无论如何,如果出于任何原因你需要强制使用英语,你可以欺骗使用standardUserDefaults第一语言的geoCoder,这样你就可以在调用geoCoder方法之前做这样的事情:

Anyway, if for any reason you need to force english, you can trick the geoCoder which uses the standardUserDefaults first language so you can do something like this just before calling the geoCoder method:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];

这样,geoCoder会以英文为您提供所有信息。

This way, geoCoder will give you all the information in english.

然而,这会改变用户偏好,因此这是将它们带回原处的最佳方法:

However, this will change the user preferences so it is a best approach to give them back to where they were:

NSMutableArray *userDefaultLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];

[self.geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray* placemarks, NSError* error){
       MKPlacemark *placemarker = [placemarks objectAtIndex:0];
       NSLog(@"%@",placemarker.locality);
   }];

[[NSUserDefaults standardUserDefaults] setObject:userDefaultLanguages forKey:@"AppleLanguages"];

正如我所说,你应该真的想到为什么你需要这个,但如果你真的需要这个,那个会工作。

As I said, you should really think why you need this, but if you really need this, that would work.

这篇关于CLPlacemark.locality,如果设备语言不同,则值会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆