ios 城市定位
前言:
获取经纬度并且转换成城市
ios8定位失败解决
获取中文城市
1、建立简单的项目, 导入coreloation.framework:

2、在info.plist中加上nslocationalwaysusagedescription值为alwayslocation:

3、使用cllocationmanager对象进行定位:
?
| 1 2 3 4 5 6 |
_locationmanger = [[cllocationmanager alloc] init];
_locationmanger.delegate = self;
[_locationmanger requestalwaysauthorization];//ios8需要加上,不然定位失败
_locationmanger.desiredaccuracy = kcllocationaccuracybest; //最精确模式
_locationmanger.distancefilter = 100.0f; //至少10米才请求一次数据
[_locationmanger startupdatinglocation]; //开始定位
|
4、在cllocationmanagerdelegate代理方法(ios8)中获取定位信息并且转换成中文城市:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
- (void)locationmanager:(cllocationmanager *)manager didfailwitherror:(nserror *)error{
nslog(@"定位失败");
[_locationmanger stopupdatinglocation];//关闭定位
}
- (void)locationmanager:(cllocationmanager *)manager didupdatelocations:(nsarray *)locations{
nslog(@"定位成功");
[_locationmanger stopupdatinglocation];//关闭定位
cllocation *newlocation = locations[0];
nslog(@"%@",[nsstring stringwithformat:@"经度:%3.5f\n纬度:%3.5f",newlocation.coordinate.latitude, newlocation.coordinate.longitude]);
// 保存 设备 的当前语言
nsmutablearray *userdefaultlanguages = [[nsuserdefaults standarduserdefaults] objectforkey:@"applelanguages"];
// 强制 成 简体中文
[[nsuserdefaults standarduserdefaults] setobject:[nsarray arraywithobjects:@"zh-hans", nil nil] forkey:@"applelanguages"];
clgeocoder * geocoder = [[clgeocoder alloc] init];
[geocoder reversegeocodelocation:newlocation completionhandler:^(nsarray *placemarks, nserror *error) {
for (clplacemark * placemark in placemarks) {
nsdictionary *test = [placemark addressdictionary];
// country(国家) state(城市) sublocality(区)
nslog(@"%@", [test objectforkey:@"state"]);
// 当前设备 在强制将城市改成 简体中文 后再还原之前的语言
[[nsuserdefaults standarduserdefaults] setobject:userdefaultlanguages forkey:@"applelanguages"];
}
}];
}
|
首次启动时,会提示是否开启定位并显示nslocationalwaysusagedescription的值:

其中字典test的具体内容是:

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/u012390519/article/details/41486793








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。