IOS之定位详解

//1.导入框架

#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>

{

 //2.声明全局的定位管理器对象

    CLLocationManager *manager;

    

    

    float allTime;

    float allDistance;

    CLLocation *lastLocation;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    /*

     1.CoreLocation框架

     2.CLLocationManager:定位管理器

     3.CLocation:保存位置信息

     */

    //3.实例化定位管理器对象

    manager = [[CLLocationManageralloc]init];

    //4.判断手机是否打开定位服务

    BOOL isOpenLocationService = [CLLocationManagerlocationServicesEnabled];

    if (isOpenLocationService ==NO) {

        NSLog(@"请到设置->隐私->定位服务里打开定位服务");

        return;

    }

//5.获取定位服务权限 (并且到info.plist进行相应配置,加一个key值,和下面对应

NSLocationWhenInUseUsageDescription  

还有一种是

NSLocationAlwaysUsageDescription,那么对应下面应该是

[manager requestAlwaysAuthorization];

)

    [manager requestWhenInUseAuthorization];

    //6.设置定位精度和频率

    //精度

    manager.desiredAccuracy =10;

    //频率

    manager.distanceFilter =10;

    //7.挂代理

    manager.delegate =self;

    //8.开始定位

    [managerstartUpdatingLocation];

    

    


}


//实现代理方法,定位成功

static int isFirst =0;

- (void)locationManager:(CLLocationManager *)manager

     didUpdateLocations:(NSArray<CLLocation *> *)locations{

    //获取到代表当前位置信息的CLLocation对象

    CLLocation *curLocation = locations.lastObject;

    NSLog(@"经度=%f \n 纬度=%f",curLocation.coordinate.latitude,curLocation.coordinate.longitude);

    NSLog(@"海拔%f",curLocation.altitude);

    if (isFirst ==0) {

        isFirst =1;

    }else{

      float  istance =  [curLocationdistanceFromLocation:lastLocation];

        allDistance +=istance;

      float time = [curLocation.timestamptimeIntervalSinceDate:lastLocation.timestamp];

        allTime += time;

    }

  

    NSLog(@"行驶了%fm行驶了%fs速度%fm/s",allDistance,allTime,allDistance/allTime);

    lastLocation = curLocation;

}

//定位失败,返回错误原因

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

    NSLog(@"%@",error);

}


附录编码反编码的方法

- (void)viewDidLoad {

    [super viewDidLoad];

    /*

     * 编码与反编码

     

     * CLGeocoder ->CoreLocation框架,实现编码与反编码

     

     * CLPlaceMark -> CoreLocation框架,存放地标信息

     

     * PS:编码与反编码较耗性能,建议放到子线程执行

     

     */

    [self geocoder:@"郑州科技学院"];

    CLLocationCoordinate2D coor ;

    coor.latitude = 31;

    coor.longitude = 113;

    [self reverseCoder:coor];

}

//编码

-(void)geocoder:(NSString *)name{

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

    dispatch_async(queue, ^{

        //创建一个编码对象

        CLGeocoder *geocoder = [[CLGeocoder alloc]init];

        //编码

        [geocoder geocodeAddressString:name completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

            CLPlacemark *placeMark = placemarks.lastObject;

           

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

            

            NSLog(@"地址信息字典=%@",placeMark.addressDictionary);

        }];

    });

}

//反编码

-(void)reverseCoder:(CLLocationCoordinate2D )coor{

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

    dispatch_async(queue, ^{

        CLGeocoder *reverseGeocoder = [[CLGeocoder alloc]init];

        CLLocation *location = [[CLLocation alloc]initWithLatitude:coor.latitude longitude:coor.longitude];

        [reverseGeocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

            CLPlacemark *placemark = placemarks.lastObject;

            NSLog(@"%@",placemark.addressDictionary);

        }];

    });

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值