CLLocation

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];

   

}

追寻最真
原文地址:https://www.cnblogs.com/zhao-jie-li/p/5841541.html