美文网首页
IOS:OC-定位

IOS:OC-定位

作者: 任任任任师艳 | 来源:发表于2017-08-18 18:45 被阅读0次

    在info.plist设置NSLocationWhenInUseageDescription的添加上去
    2.托两个button

    import "ViewController.h"

    //定位框架 定位框架中的类都是Cl开头的

    import <CoreLocation/CoreLocation.h>

    @interface ViewController ()<CLLocationManagerDelegate>
    //定义专门负责定位的类
    @property(nonatomic,strong)CLLocationManager * locationManager;
    //定义专门负责地理位置编码的类(就是讲经纬度转为具体的地址或者将地址转为经纬度)
    @property(nonatomic,strong)CLGeocoder * geocoder;

    @end

    @implementation ViewController

    • (void)viewDidLoad {
      [super viewDidLoad];
    //1.定位第一步 在infer.plist文件中设置支持定位的字段
    //使用期间允许定位 NSLocationWhenInUseageDescription
    //始终允许定位 NSLocationAlwaysUsageDescription
    
    //2.创建定位管理对象
    self.locationManager = [[CLLocationManager alloc] init];
    
    //3.判断手机的定位服务状态
    if (![CLLocationManager locationServicesEnabled]) {
        //提示用户开启首服务状态
        NSLog(@"手机设置中的定位服务没有开启");
        //跳转手机的设置界面
        NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] canOpenURL:url];
    }
    
    //4.判断当前软件是否支持定位,也就是判断用户的授权状态
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        //kCLAuthorizationStatusNotDetermined 是无授权状态
        //请求当前程序运行时允许定位这种授权
        [self.locationManager requestWhenInUseAuthorization];
       
    }
    //5.开始定位
    //设置相关属性
    //设置定位的精确度 越精确越耗电
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //设置最小更新距离,就是一旦超过这个距离就会重新再定位(导航中必设置)
    self.locationManager.distanceFilter = 100;
    
    //6 设置代理对象--遵循协议^^^^^
    self.locationManager.delegate = self;
    
    //7.开启服务
    [self.locationManager startUpdatingLocation];
    
    //***********************************
    //开空间
    self.geocoder = [[CLGeocoder alloc] init];
    
    
    [self distance];
    

    }
    //正向地理编码 通过地址获取其经纬度

    • (IBAction)addressToCoordinate:(UIButton *)sender {
      //先看正向
      [self.geocoder geocodeAddressString:@"内蒙古自治区" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
      //placemarks地标对象 存储数据的范围比CLLocation更大
      CLPlacemark * mark = [placemarks firstObject];
      //获取经纬度
      CLLocationCoordinate2D coordinate = mark.location.coordinate;
      NSLog(@"经度%f,纬度%f",coordinate.longitude,coordinate.latitude);
    }];
    

    }
    //反向地理编码 通过经纬度获取其地址

    • (IBAction)coordinateToAddress:(UIButton *)sender {
      //
      double longitude = 111.670801;
      double latitude = 40.818311;
      //反向编码
      //先获取一下经纬度结构体变量 以及位置对像
      CLLocation * location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
      [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
      //取地标
      CLPlacemark *mark = [placemarks firstObject];
      //所有相关信息 都在线面字典中
      NSDictionary * dic = mark.addressDictionary;
      //例如 其中有个key叫做 address
      NSLog(@"%@",dic);
      NSLog(@"%@",[dic objectForKey:@"City"]);
      NSLog(@"%@",[dic objectForKey:@"Name"]);

      }];

    }

    pragma Mark:---定位的代理方法

    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"定位失败");
    }
    //定位成功或者再次定位 定位成功的时候执行回调的方法
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    //注意 CLLocation类是 位置类 专门存储位置信息
    CLLocation * location = [locations firstObject];
    //因为 此方法会执行很多次 所以存储位置的是一个数组容器 会吧最新的位置信息存储为第一个对象
    //存储经纬度的是一个结构体
    CLLocationCoordinate2D coordinate = location.coordinate;
    NSLog(@"经度:%f,纬度%f,海拔:%f,速度%f,航向%f",coordinate.longitude,coordinate.latitude,location.altitude,location.speed,location.course);
    //最后一遍 所有跟位置有关的都是 CLLocation存储的

    }

    pragma Mark:--计算两点间的距离

    -(void)distance{
    CLLocation * lon1 = [[CLLocation alloc] initWithLatitude:40.34 longitude:111.45];
    CLLocation * lon2 = [[CLLocation alloc] initWithLatitude:42.34 longitude:111.45];
    //计算距离
    CLLocationDistance distance = [lon1 distanceFromLocation:lon2];
    NSLog(@"两个位置的距离为%f",distance);

    }

    相关文章

      网友评论

          本文标题:IOS:OC-定位

          本文链接:https://www.haomeiwen.com/subject/olaarxtx.html