本文转载至 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. .net的远程调用

    .Net远程调用(转自:http://www.cnblogs.com/omilan/articles/3191378.html) 看到了这.net远程调用的讲解,觉得不错,拿来分享!! .Net对于远 ...

  2. domino数据同步到sql server

          近期有个需求,要同步domino数据到selservlet数据库,查看几年前ls使用odbc写的同步功能,还能正常使用.而且加入读者域进去功能(之前没有这个需求).改进成网页配置版本号(曾 ...

  3. Nginx include和Nginx指令的使用

    Nginx include和Nginx指令的使用 1.nginx include 主配置文件nginx.conf中指定包含其他扩展配置文件,从而简化nginx主配置文件,实现多个站点功能 [root@ ...

  4. ECSHOP后台开发模块步骤

    一.建数据库二.添加到后台导航栏并配置相关语言包三.权限配置四.添加增删查改五.增加其他功能(复制,搜索(暂时调不出来页面),排序,转移,AJAX) 以添加支付信息模块为例 第一步首先我们用phpmy ...

  5. [React] Use react-rewards to add microinteractions to React app to reward users for some actions

    It's important that our users enjoy using our application or website. One way we can make it happen ...

  6. EffectiveJava(6)消除过期对象的引用

    消除过期对象的引用 过期引用 – 在操作集合类的时候,未清理过期的对象引用(即:过期引用),常会导致内存泄露.从而报outOfmemory错误. 1.过期对象导致内存泄漏. 2.一旦对象引用过期,清除 ...

  7. EffectiveJava(5)避免创建不必要的对象

    避免创建不必要的对象 1.通过延迟初始化对象提高性能 调用功能方法时调用静态工厂方法,而不是调用类时使用 2.适配器:把功能委托给一个后备对象,从而为后备对象提供一个接口的对象 3.自动装箱:优先使用 ...

  8. Node.js 极简入门Helloworld版服务器例子

    粗浅得很,纯属备忘. // 内置http模块,提供了http服务器和客户端功能(path模块也是内置模块,而mime是附加模块) var http=require("http"); ...

  9. ubuntu用su切换,输入密码提示认证失败解决办法

    ubuntu用su切换,输入密码提示认证失败,经查阅原来Ubuntu安装后,root用户默认是被锁定了的,不允许登录,也不允许 su 到 root ,对于桌面用户来说这个可能是为了增强安全性. 终端下 ...

  10. VB程序无法运行,Component ‘MCI32.OCX’错误怎么办

    1 提示Component 'MCI32.OCX'错误   2 搜索你电脑的MCI32.OCX这个文件   3 把它复制到任意位置,然后再同一个目录下新建一个文本文档,输入regsvr32 MCI32 ...