博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CLLocation
阅读量:7205 次
发布时间:2019-06-29

本文共 3793 字,大约阅读时间需要 12 分钟。

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
*)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
*)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]; }

转载于:https://www.cnblogs.com/zhao-jie-li/p/5841541.html

你可能感兴趣的文章
将Word文档转化为HTML格式的文档
查看>>
WCF
查看>>
IDE安装Lombok插件提高开发效率
查看>>
转://使用showplan.sql分析sql Performance
查看>>
第七课:增量分析
查看>>
【总结整理】JQuery基础学习---动画
查看>>
A. Round House_数学问题
查看>>
LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)
查看>>
0920编译原理词法分析
查看>>
浅谈数位DP的dfs写法
查看>>
Linux下的bash对拍
查看>>
C++ STL标准入门
查看>>
移动端页面制作2
查看>>
UE4中的常量材质实例和动态材质实例
查看>>
php中的字符串和正则表达式
查看>>
SharePoint获取UserProfile的信息
查看>>
[题解]第十一届北航程序设计竞赛预赛——I.神奇宝贝大师
查看>>
Python--关于连接符+
查看>>
office 所有后缀对应的 content-type
查看>>
Linux 第十四天
查看>>