本文转自: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下的定位授权》

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

  1. iOS8中的定位服务

    iOS8中的定位服务 My app that worked fine in iOS 7 doesn't work with the iOS 8 SDK. CLLocationManager doesn ...

  2. iOS8中定位服务的变化(CLLocationManager协议方法不响应,无法回掉GPS方法,不出现获取权限提示)

    最近在写一个LBS的项目的时候,因为考虑到适配iOS8,就将项目迁移到Xcode6.0.1上,出现了不能正常获取定位服务权限的问题. self.manger = [[CLLocationManager ...

  3. 系统定位在iOS8中的改变

    CLLocationManager这个系统定位的类在iOS8之前要实现定位,只需要遵守CLLocationManagerDelegate这个代理即可: - (void)startLocate {   ...

  4. CoreLocation定位技术

    CoreLocation框架可用于定位设备当前经纬度,通过该框架,应用程序可通过附近的蜂窝基站,WIFI信号或者GPS等信息计算用户位置.      iOS定位支持的3种模式.      (1)GPS ...

  5. iOS - CoreLocation 定位

    前言 NS_CLASS_AVAILABLE(10_6, 2_0) @interface CLLocationManager : NSObject 1.CoreLocation 定位 配置 1.在 iO ...

  6. CoreLocation 定位

    前言: 本章会使用OC和Swift分别进行实现,需要了解Swift的小伙伴可以翻一下之前的博文 LBS和SoloMo(索罗门) LBS:基于位置的服务,根据定位展示周边美食.景点等信息(全称:Loca ...

  7. [译]一个灵活的 Trello 敏捷工作流

    [译]一个灵活的 Trello 敏捷工作流 翻译自 An Agile Trello Workflow That Keeps Tasks Flexible Getting things done 可不只 ...

  8. 使用系统的CoreLocation定位

    ////  ViewController.m//  LBS////  Created by tonnyhuang on 15/8/28.//  Copyright (c) 2015年 tonnyhua ...

  9. 关于CoreLocation定位服务的简单使用

    在我们发微博,发表空间内容,以及在朋友圈发表动态的时候,会发现有一个位置信息的控件.iOS中是如何定位我们的位置信息的呢?基于此写一个小Demo,供大家参考使用. 在iOS中,用于定位时需要我们导入以 ...

随机推荐

  1. Apache Mina(一)

    原文链接:http://www.cnblogs.com/xuekyo/archive/2013/03/06/2945826.html Apache Mina是一个能够帮助用户开发高性能和高伸缩性网络应 ...

  2. 作业七:团队项目——Alpha版本冲刺阶段-02

    昨天进展:框架设计以及菜单设计. 今天安排:完善界面设计以及象棋图片的绘制. 小组一共三人,陈芝航因家里有事,与我们进行了QQ视屏会议.

  3. [ACM_搜索] POJ 1096 Space Station Shielding (搜索 + 洪泛算法Flood_Fill)

    Description Roger Wilco is in charge of the design of a low orbiting space station for the planet Ma ...

  4. SharePoint 2013 Service 状态无法启动,显示“启动中(Starting)”

    Problem 在SharePoint 2013 Central Administration中启动 SharePoint Service(也称为:Service Machine Instance)时 ...

  5. BUILD 2015: Visual Studio对GitHub的支持

    微软BUILD 2015大会上发布了Visual Studio 对GitHub的支持.安装了如下Developer Assistant插件后,你便可以在Visual Studio中找到GitHub上的 ...

  6. AngularJS快速入门指南10:DOM节点

    AngularJS通过指令将application数据绑定到HTML DOM元素的属性上. ng-disabled指令 ng-disabled指令将AngularJS application数据绑定到 ...

  7. paip.提升用户体验--提升java的热部署热更新能力

    paip.提升用户体验--提升java的热部署热更新能力 想让java做到php那么好的热部署能力  "fix online"/在线修复吗??直接在服务器上修改源码生效,无需重启应 ...

  8. Leetcode 326 Power of Three 数论

    判断一个数是否是3的n次幂 这里我用了一点巧,所有的int范围的3的n次幂是int范围最大的3的n次幂数(即3^((int)log3(MAXINT)) =  1162261467)的约数 这种方法是我 ...

  9. 定义一个时钟类——Clock,它包括三个int型 成员变量分别表示时、分、秒,一个构造方法用于对三个成员变量(时、分、秒) 进行初始化,还有一个成员方法show()用于显示时钟对象的时间。其次,再定义 一个主类——TestClass,在主类的main方法中创建多个时钟类的对象,使用这 些对象调用方法show()来显示时钟的时间

    package java1; public class Clock { int hhh; int mmm; int sss; Clock(int h,int m,int s) { hhh=h; mmm ...

  10. iOS10之后openURL:方法过期之后的替代方法及使用

    目前苹果为iOS10开放了一个key:UIApplicationOpenURLOptionUniversalLinksOnly但亲测无效 目前使用的是这个key:UIApplicationOpenUR ...