http://blog.sina.com.cn/s/blog_9e8867eb01013knc.html 这家伙写的不错本人也参考了这篇博客,希望原文博主可以谅解新手的无奈举措

首相要提到的类是 CLLocationManager 这个类 英文描述如下

The CLLocationManager class is the central point for configuring the delivery of location- and heading-related events to your app. You use an instance of this class to establish the parameters that determine when location and heading events should be delivered and to start and stop the actual delivery of those events. You can also use a location manager object to retrieve the most recent location and heading data.

CLLocationManager 类是配置到您的应用程序的位置和标题相关的事件交互的关键。您可以使用此类的实例建立的参数的确定位置和事件的标题在交互时,启动和停止这些事件的实交 互。也可以使用一个位置管理器对象来检索标题数据与最近的位置。简单的说就是可以实现定位管理的功能

定位的3种方式:

1.GPS ,最精确的定位方式 (原博主说iPhone1不支持)

2.蜂窝三角定位,(应该是利用数据来定位的,没有涉猎到这部分知识,回头补上:DOTO? 依靠基站比较密集的城市会比较准确)

3.WiFi,

使用如下:

首先要有一个CLLocationManager这个类作为属性 来管理定位

要使用CLLocationManager这个类就必须遵守这个类的协议

遵守协议实现如下的方法:

1.监控用户是否同意开启定位功能(此操作应该是iOS8以后才有的,后面当然也有iOS8之前的定位操作)

 - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;

参数status是一个枚举,如下:

kCLAuthorizationStatusNotDetermined  不确定,不知道什么时候是使用

kCLAuthorizationStatusRestricted  有限制的使用

kCLAuthorizationStatusDenied   不使用

kCLAuthorizationStatusAuthorizedAlways  总是使用

kCLAuthorizationStatusAuthorizedWhenInUse   在使用的情况下开启

kCLAuthorizationStatusAuthorized   自动开启

2.监听已经定位到得用户的位置(前提是已经开启定位服务)

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations;

参数locations 是一个数组 记录了位置的变化信息,其中数组中的最后一个对象是最新的位置信息,也就是目前的位置信息

由方法可以知道数组中的元素的类型是CLLocation类型的 可以的得到其中的元素,CLLocation类型的对象有一个坐标属性叫做(coordinate)类型是CLLocationCoordinate2D 是一个结构体 其中包含了精度和纬度(double型)

CLLocationDegrees latitude;维度

CLLocationDegrees longitude; 经度

使用方式

引入CoreLocation文件包 ,需要手动导入

需要CLLocationManager来开启定位服务,定位服务是轮询的,为对程序来说是需要一定时间才会得到的(表示不懂什么意思),所以CLLocationManager的操作都交给代理来完成,设置当前的控制器为代理

4.操作步骤

创建位置管理Manager对象

self.locationManager = [[CLLocationManager alloc] init];

将控制器设置为代理

locationManager.delegate=self;
//为设置定位的精度,可以设为最优,装置会自动用最精确的方式去定位
    self.mgr.desiredAccuracy=kCLLocationAccuracyBest;
//distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序,它的单位是米
self.mgr.distanceFilter=100.0f;

下面来判断当前的iOS版本,来选择不同的定位方式

 #define  kIOSVersion [[UIDevice currentDevice].systemVersion doubleValue]

if (kIOSVersion>8.0) {

        //选择使用方式 +征求意见(info.plist)

如果是大于iOS8的,就需要手动设置plist文件

苹果文档是这么说的:

*      If the NSLocationWhenInUseUsageDescription key is not specified in your

 *      Info.plist, this method will do nothing, as your app will be assumed not

 *      to support WhenInUse authorization.

 */

//           - (void)requestWhenInUseAuthorization

        [self.mgr requestWhenInUseAuthorization];

    }else{

        //本版低于iOS8

        [self.mgr startUpdatingLocation ];

    }

//监听用户的操作:是否同意定位

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{

    switch (status) {

            //拒绝

        case kCLAuthorizationStatusDenied:

            NSLog(@"用户不允许定位");

            break;

        case kCLAuthorizationStatusAuthorizedWhenInUse:

           [ self.mgr startUpdatingLocation];

            break;

        default:

            break;

    }

}

//监听已经定位到得用户的位置(用户位置已知,说明已经定位到了)(不会的方法直接option键查看)

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

    //注意1:返回的位置是数组 (最后一项是最新的位置)

    CLLocation *latestLocation=locations.lastObject;

    //注意2:CLLocation中coordinate(坐标位置)

    CLLocationCoordinate2D coordinate=latestLocation.coordinate;

    NSInteger latitude=coordinate.latitude;

    NSInteger longitude=coordinate.longitude;

    NSLog(@"latitude:%ld  longitude %ld",(long)latitude,(long)longitude);

    //如果只定位1次

    self.mgr=nil;

    //结束定位

    [self.mgr stopUpdatingLocation];

}

CLLocation的更多相关文章

  1. iOS地图 -- 定位中的CLLocation的介绍与小练习

    通过定位练习,熟悉CLLocation 在上篇笔记中提到了CLLocation类,这里通过练习来讲解一下这个类,类中包含了获取到的用户位置的信息 coordinate --> 坐标,经度和纬度 ...

  2. 获取经纬度 CLLocation

    //导入库 #import <CoreLocation/CoreLocation.h> //注意: //需要在 info.plist 中导入前两个字段 //NSLocationAlways ...

  3. CLLocation的属性以及使用的解释

    http://blog.csdn.net/u012496940/article/details/47405345  上一篇的链接(一个定位实例) 从上一篇中的实例了解所使用的一些元素: CLLcati ...

  4. iOS 原生地图地理编码与反地理编码

    当我们要在App实现功能:输入地名,编码为经纬度,实现导航功能. 那么,我需要用到原生地图中的地理编码功能,而在Core Location中主要包含了定位.地理编码(包括反编码)功能. 在文件中导入 ...

  5. iOS开发系列--地图与定位

    概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...

  6. iOS之获取经纬度并通过反向地理编码获取详细地址

    _locationManager = [[CLLocationManager alloc] init]; //期望的经度 _locationManager.desiredAccuracy = kCLL ...

  7. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  8. iOS项目iCloud及CloudKit Dashboard运用

    CloudKit是苹果推出的基于iCloud的一个云端数据存储服务.其 主要由下面两部分组成: 一个仪表web页面,用于管理公开数据的记录类型. 一组API接口,用于iCloud和设备之间的数据传递. ...

  9. 定位框一闪而过 iOS Swift

    需求:获取经纬度. 方案:我自定义了一个类模块CLLocationModule.swift 备注以下代码里 let IS_IOS8 = (UIDevice.currentDevice().system ...

随机推荐

  1. 成都UBER优步司机第六组奖励政策

    保底时段详解 滴滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs. ...

  2. .NET(C#):使用反射来获取枚举的名称、值和特性【转】

    首先需要从内部了解一下枚举(Enumeration),相信许多人已经知道了,当我们声明一个这样的枚举类型: enumMyEnum { AAA, BBB, CCC } 背后的IL是这样的: .class ...

  3. STL 源代码剖析 算法 stl_algo.h -- random_shuffle

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie random_shuffle ------------------------------- ...

  4. Spring 整合hibernante 错误java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    1.将所需的jar包全部拷贝到WEB-INF/lib下,再重新启动tomcat便能顺利通过了.参考http://blessht.iteye.com/blog/1104450 最佳答案   spring ...

  5. whereis linux文件搜索

    whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis查找的速度非 ...

  6. MySql Error 2006

    导入长字段时出现2006错误 在my.ini最后添加 max_allowed_packet = 10M 问题解决. max_allowed_packet 参数的作用是,用来控制其通信缓冲区的最大长度.

  7. [置顶] 蓝牙基础知识进阶——Physical channel

    从本篇文章开始,晓东将会和大家一起来学习一些蓝牙的比较高阶的基础知识. 二.物理通道 物理通道是piconet区分的标准,它是蓝牙系统结构层次中的最底层了.     Q1:物理通道有哪些类型 物理通道 ...

  8. less的学习(css)

    因为新公司需要用less来写样式,对于用惯了css的我来说还是觉得有点麻烦 但是呢,都是有个过程嘛,学习必须走起嘛. 写到半中央发现一个写的特别好的less帖子,就不写. http://www.w3c ...

  9. 关于ie6中使用css滤镜[_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/*.png',sizingMethod='scale')]后链接无法点击的问题

    RT,我做的一个效果是试用png图做背景,通过_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/*.png' ...

  10. struts2笔记05-ServletActionContext

    1.ServletActionContext ServletActionContext, 这个类继承自ActionContext, 所以它具有ActionContext的很多功能,不过更重要的是它提供 ...