CoreLocation框架的使用---定位,求两地距离

前言:

在iOS开发中,有关导航,周边的开发,必须基于2个框架:

Map Kit :用于地图展示
Core Location :用于地理定位
 

用户隐私的保护

从iOS 6开始,苹果在保护用户隐私方面做了很大的加强,以下操作都必须经过用户批准授权:获取用户的位置、通讯录、日历、相机、相册等等
从iOS 8开始,用户定位分两种情况
总是使用用户位置:NSLocationAlwaysUsageDescription
使用应用时定位:NSLocationWhenInUseDescription               [self.manager requestWhenInUseAuthorization];
当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权
      

CoreLocation框架的使用

1.导入框架CoreLocation.framework

      

2.导入主头文件

  #import <CoreLocation/CoreLocation.h>

3.在info.plist文件中设置提示用户定位信息,NSLocationAlwaysUsageDescription

      

4.代码: 功能定位当前经纬度. 一直两地经纬度,求距离

    

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>//遵守代理
@property (nonatomic, strong) CLLocationManager *manager;

@end

@implementation ViewController
- (CLLocationManager *)manager
{
    if (!_manager) {
        //1.创建一个定位管理器
        _manager =  [[CLLocationManager alloc]init];
    }
    
    return _manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //2.获取用户的授权
    [self.manager requestAlwaysAuthorization];
    
    //3.开始定位
    [self.manager startUpdatingLocation];
    
    //4.设置代理
    self.manager.delegate = self;
    
    //5.求两地距离.
    CLLocation *loc1 = [[CLLocation alloc]initWithLatitude:40 longitude:116];
    CLLocation *loc2 = [[CLLocation alloc]initWithLatitude:41 longitude:116];
    
    CLLocationDistance dis =  [loc1 distanceFromLocation:loc2];
    NSLog(@"两地距离是%f米",dis);
    
}

//代理方法:会一直调用此方法.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    //6.获取用户当前的位置
    CLLocation *location = [locations firstObject];
    
    /*经纬度 CLLocationCoordinate2D是一个经纬度的结构体.
     coordinate.longitude  经度
     coordinate.latitude   纬度
     */

    CLLocationCoordinate2D coordinate =  location.coordinate;
    
    NSLog(@"纬度:%f ----经度 %f",coordinate.latitude,coordinate.longitude);

}

CLLocationManager

- (void)startUpdatingLocation;//开始用户定位

- (void) stopUpdatingLocation;//停止用户定位

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;//代理方法,当调用了startUpdatingLocation方法后,就开始不断地定位用户的位置,中途会频繁地调用. locations参数里面装着CLLocation对象 //参考上边代码
 
@property(assign, nonatomic) CLLocationDistance distanceFilter;//每隔多少米定位一次
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;//定位精确度(越精确就越耗电)
     //导航专用的精确度
     extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);
     extern const CLLocationAccuracy kCLLocationAccuracyBest;
     extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
     extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
     extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
     extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
    */
    //定位精确度
    self.manager.desiredAccuracy =  kCLLocationAccuracyBestForNavigation;

CLLocation

CLLocation用来表示某个位置的地理信息,比如经纬度、海拔等等
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;//经纬度
@property(readonly, nonatomic) CLLocationDistance altitude;//海拔
@property(readonly, nonatomic) CLLocationDirection course;//路线,航向(取值范围是0.0° ~ 359.9°,0.0°代表真北方向)
p@property(readonly, nonatomic) CLLocationSpeed speed;//行走速度(单位是m/s)
用- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location   方法可以计算2个位置之间的距离

CLLocationCoordinate2D

CLLocationCoordinate2D是一个用来表示经纬度的结构体,定义如下

typedef struct {

        CLLocationDegrees latitude; // 纬度

        CLLocationDegrees longitude; // 经度

} CLLocationCoordinate2D;

一般用CLLocationCoordinate2DMake函数来创建CLLocationCoordinate2D
 
模拟器当前位置(经纬度)设定:
      
原文地址:https://www.cnblogs.com/jiayongqiang/p/5584476.html