iOS8中使用CoreLocation定位[转]

本文转自:http://blog.devzeng.com/blog/ios8-corelocation-framework.html

iOS8以前使用CoreLocation定位

1、首先定义一个全局的变量用来记录CLLocationManager对象,引入CoreLocation.framework使用#import <CoreLocation/CoreLocation.h>

   @property (nonatomic, strong) CLLocationManager *locationManager;

2、初始化CLLocationManager并开始定位

self.locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 10;
[_locationManager startUpdatingLocation];

3、实现CLLocationManagerDelegate的代理方法

(1)获取到位置数据,返回的是一个CLLocation的数组,一般使用其中的一个

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *currLocation = [locations lastObject];
    NSLog(@"经度=%f 纬度=%f 高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);
}

(2)获取用户位置数据失败的回调方法,在此通知用户

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    if ([error code] == kCLErrorDenied)
    {
        //访问被拒绝
    }
    if ([error code] == kCLErrorLocationUnknown) {
        //无法获取位置信息
    }
}

4、在viewWillDisappear关闭定位

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_locationManager stopUpdatingLocation];
}

iOS8中使用CoreLocation定位

1、在使用CoreLocation前需要调用如下函数【iOS8专用】:

iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法:

(1)始终允许访问位置信息

- (void)requestAlwaysAuthorization;

(2)使用应用程序期间允许访问位置数据

- (void)requestWhenInUseAuthorization;

示例如下:

self.locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 10;
[_locationManager requestAlwaysAuthorization];//添加这句
[_locationManager startUpdatingLocation];

2、在Info.plist文件中添加如下配置:

(1)NSLocationAlwaysUsageDescription

(2)NSLocationWhenInUseUsageDescription

这两个键的值就是授权alert的描述,示例配置如下[勾选Show Raw Keys/Values后进行添加]:


参考资料

1、《迎接iOS8 – CoreLocation的变化》

2、《IOS开发之Core Location》

3、《IOS8下的定位授权》

时间: 2024-08-03 07:11:19

iOS8中使用CoreLocation定位[转]的相关文章

iOS8中使用CoreLocation定位

在iOS8中,苹果已经强制开发者在请求定位服务时获得用户的授权,此外iOS状态栏中还有指示图标,提示用户当前应用是否正在使用定位服务.另外在iOS8中,苹果进一步改善了定位服务,让开发者请求定位服务时需要向用户提供更多的透明.此外,iOS8中还支持让应用开发者调用全新的"访问监控"功能,当用户允许后应用才能获得更多的定位数据. iOS8以前使用CoreLocation定位 1.首先定义一个全局的变量用来记录CLLocationManager对象,引入CoreLocation.frame

iOS8以上使用CoreLocation定位

1.在使用CoreLocation前需要调用如下函数 iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法: (1)始终允许访问位置信息 - (void)requestAlwaysAuthorization; (2)使用应用程序期间允许访问位置数据 - (void)requestWhenInUseAuthorization; 示例如下: self.locationManager = [[CLLocationManager alloc]ini

iOS8以前与iOS8使用CoreLocation定位

iOS8以前使用CoreLocation定位1.首先定义一个全局的变量用来记录CLLocationManager对象,引入CoreLocation.framework使用#import <CoreLocation/CoreLocation.h>1 @property (nonatomic, strong) CLLocationManager *locationManager; 2.初始化CLLocationManager并开始定位 -(CLLocationManager *)locationM

第九章:使用CoreLocation定位

CoreLocation框架(CoreLocation.framework)可用于定位设备当前的经纬度,通过该框架,应用程序可通过附近的蜂窝基站.WIFI信号或者GPS等信息计算用户位置. iOS SDK提供了CLLocationManager.CLLocationManagerDelegate来处理设备的定位信息,包括获取设备的方向以及进行方向检测等.其中CLLocationManager是整个CoreLocation框架的核心,定位.方向检测.区域检测等都由该API完成:而CLLocatio

[翻译]The Neophyte&#39;s Guide to Scala Part 12: Type Classes

The Neophyte's Guide to Scala Part 12: Type Classes 过去的两周我们讨论了一些使我们保持DRY和灵活性的函数式编程技术,特别是函数组合,partial function的应用,以及currying.接下来,我将会继续讨论如何使你的代码尽可能的灵活. 但是,这次我们将不会讨论怎么使用函数作为一等对象来达到这个目的,而是使用类型系统,这次它不是阻碍着我们,而是使得我们的代码更灵活:你将会学到关于 type classes 的知识. 你可能会觉得这是一

iOS8中的定位服务

My app that worked fine in iOS 7 doesn't work with the iOS 8 SDK. CLLocationManager doesn't return a location, and I don't see my app under Settings > Location Services either. I did a Google search on the issue but nothing came up, what could be wro

系统定位在iOS8中的改变

CLLocationManager这个系统定位的类在iOS8之前要实现定位,只需要遵守CLLocationManagerDelegate这个代理即可: - (void)startLocate {     if([CLLocationManager locationServicesEnabled]){ _locManager = [[CLLocationManager alloc]init]; [self.locManager setDelegate:self]; [self.locManager

[转]在iOS项目中使用CorePlot框架

转载地址:http://blog.csdn.net/llfjfz/article/details/7849190#comments Core Plot是OS X和IOS下的一个开源图形库,它提供数据的可视化处理,就是画曲线图.柱状图和饼图等等.如何在项目中使用Core Plot的静态库呢?以下是几个步骤: 首先先去Google Code下载Core Plot图形库,网址 http://code.google.com/p/core-plot/ .目前该网址提供了CorePlot_1.0.zip下载

[转载]矩阵及变换,以及矩阵在DirectX和OpenGL中的运用问题:左乘/右乘,行优先/列优先

[转载]http://www.xuebuyuan.com/882848.html (一)首先,无论dx还是opengl,所表示的矢量和矩阵都是依据线性代数中的标准定义的:“矩阵A与B的乘积矩阵C的第i行第j列的元素c(ij)等于A的第i行于B的第j列的对应元素乘积的和.”(实用数学手册,科学出版社,第二版)例如c12 = a11*b11+a12*b21+a12*b13... (二)在明确了这一点后,然后我们再看“矩阵的存储方式”,矩阵存储方式有两种,一种是“行主序(row-major order