本文转载至 http://www.tuicool.com/articles/7JBRZn

在iOS8以前的版本中,我们使用CLLocationManager定位是没有问题的,最近在iOS8系统中却无法定位了。。。。这是一大问题啊!

1、首先定义一个全局的变量用来记录CLLocationManager对象,引入CoreLocation.framework使用#import <CoreLocation/CoreLocation.h>
@property (nonatomic, strong) CLLocationManager  *locationManager;
2、初始化CLLocationManager并开始定位
locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=10;
[locationManager startUpdatingLocation];//开启定位
3、实现CLLocationManagerDelegate的代理方法
#pragma mark CLLocationManagerDelegate
/**
* 获取经纬度
*/
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *currLocation=[locations lastObject];
location.strLatitude=[NSString stringWithFormat:@"%f",currLocation.coordinate.latitude];
location.strLongitude=[NSString stringWithFormat:@"%f",currLocation.coordinate.longitude];
NSLog(@"la---%f, lo---%f",currLocation.coordinate.latitude,currLocation.coordinate.longitude);
}
/**
*定位失败,回调此方法
*/
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
if ([error code]==kCLErrorDenied) {
NSLog(@"访问被拒绝");
}
if ([error code]==kCLErrorLocationUnknown) {
NSLog(@"无法获取位置信息");
}
}

iOS8中使用CoreLocation定位

1、在使用CoreLocation前需要调用如下函数【iOS8专用】:
iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法:
(1)始终允许访问位置信息
- (void)requestAlwaysAuthorization;
(2)使用应用程序期间允许访问位置数据
- (void)requestWhenInUseAuthorization;
   示例如下:
locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=10;
if (iOSVersion>=8) {
[locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据(iOS8定位需要)
}
[locationManager startUpdatingLocation];//开启定位
2、在Info.plist文件中添加如下配置:
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription

这样添加后,定位功能就能正常使用了!

iOS CLLocationManager定位的更多相关文章

  1. iOS CLLocationManager 定位

    今天写个定位,本来很简单,但是在填写plist时,通过系统提示,只能看到NSLocationUsageDescription项目,根本不提示 (1)NSLocationAlwaysUsageDescr ...

  2. IOS CLLocationManager定位反编码位置信息

    //获取位置和坐标#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1        if (IOS_VERSION >= 8.0) {   ...

  3. iOS地图 -- 定位初使用

    iOS的定位服务用到的框架是#import <CoreLocation/CoreLocation.h> 定位中用到的类是CLLocationManager 一.iOS8.0之前的定位 向用 ...

  4. iOS后台定位实现

    iOS后台定位实现 (2013-01-24 16:43:12)     工作中碰到一个定位的应用场景:app需要在后台运行,实时上传用户地理位置.   苹果对iOS的规范性在提升了app的品质的同时也 ...

  5. 在iOS8下使用CLLocationManager定位服务需要系统授权

    最近在ios8.0使用CLLocationManager定位服务,发现老不能定位,查看设置菜单中的项也是处于未知状态.想起之前都有一个弹出框提示用户是否允许定位,这次一直没有出现了.原来ios8.0下 ...

  6. iOS 后台定位被拒注意事项

    iOS 后台定位被拒的原因很简单就是没有达到苹果对后台定位的要求. 本地要求: 1.在plist文件中添加字段 "Privacy - Location Always Usage Descri ...

  7. iOS 地图定位及大头针的基本使用

    地图 Part1 - 定位及大头针的基本使用 一.MapKit 作用 : 用于地图展示 如大头针,路线,覆盖层展示等(着重界面展示) 使用步骤 导入头文件 #import <MapKit/Map ...

  8. 简易的IOS位置定位服务

    有时一些小的需求,其实只是需要得知当前IOS APP使用的地点,有些只是想精确到城市级别,并不需要任何地图. 有了以下的简易实现: @interface MainViewController ()&l ...

  9. iOS后台定位,实时向服务器发送最新位置

    第一步,开启后台模式,选中定位,选择project --> capabilities-->Backgorund Modes --> Location updates 如图: Past ...

随机推荐

  1. etcd集群日常维护

    配置文件和启动参数说明 命令行 | 配置文件 | 说明 data-dir | ETCD_DATA_DIR | 指定节点的数据存储目录,包括节点ID,集群ID,集群初始化配置,Snapshot文件,若未 ...

  2. centos7 安装LNMP(php7)之 nginx php-fpm yum安装以及配置文件修改

    PHP7.1.6整体参考 https://www.zhihu.com/question/50615606/answer/145699091 http://www.bubuko.com/infodeta ...

  3. django开发环境部署之pip、virtualenv、virtualenvwrapper

    step1:安装pip 在python中可以使用easy_install和pip安装python拓展但推荐使用pip Don't use easy_install, unless you like s ...

  4. 什么是滤波器,滤波器是干嘛用的,IIR和FIR滤波器又是什么?(回答请简洁,别浪费大家时间)

    信号频率筛选器,用于阻挡不需要的信号: 低通滤波用来阻挡某一频率之上的所有信号: 高通滤波用来阻挡某一频率之下的所有信号: 另外还有带通滤波器,带阻滤波器..... 个人理解,就像是个过滤沙子的筛子一 ...

  5. 直方图均衡(HE)与局部色调映射(LTM) .

    直方图均衡(Histogram Equalization)是图像处理中一个十分基础的概念,具有调整图像灰度,增强对比度的作用.    限制对比度自适应直方图均衡(Contrast Limited Ad ...

  6. hibernate中对象的3种状态:瞬时态(Transient)、 持久态(Persistent)、脱管态(Detached)

    Hibernate的对象有3种状态,分别为:瞬时态(Transient). 持久态(Persistent).脱管态(Detached). 处于持久态的对象也称为PO(Persistence Objec ...

  7. Linux如何查找大文件

    https://www.cnblogs.com/kerrycode/p/4391859.html find . -type f -size +800M 如上命令所示,我们仅仅能看到超过800M大小的文 ...

  8. Github 的一个免费编程书籍列表

    Index Ada Agda Alef Android APL Arduino ASP.NET MVC Assembly Language Non-X86 AutoHotkey Autotools A ...

  9. 通过Navicat for MySQL远程连接的时候报错mysql 1130 的解决方法

    用Navicat连接远程MYSQL,提示如下错误,我以为是自己的防火墙问题,但是关了,依然不行. ERROR 1130: Host '192.168.1.3' is not allowed to co ...

  10. ACM-百度之星资格赛之Labyrinth——hdu4826

    Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...