IOS之定位详解

来源:互联网 发布:mac 数据库建模软件 编辑:程序博客网 时间:2024/05/23 23:00

//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 {

    [superviewDidLoad];

    /*

     * 编码与反编码

     

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

     

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

     

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

     

     */

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

    CLLocationCoordinate2D coor ;

    coor.latitude =31;

    coor.longitude =113;

    [selfreverseCoder:coor];

}

//编码

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

    dispatch_queue_t queue =dispatch_get_global_queue(0,0);

    dispatch_async(queue, ^{

        //创建一个编码对象

        CLGeocoder *geocoder = [[CLGeocoderalloc]init];

        //编码

        [geocoder geocodeAddressString:namecompletionHandler:^(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 = [[CLGeocoderalloc]init];

        CLLocation *location = [[CLLocationalloc]initWithLatitude:coor.latitudelongitude:coor.longitude];

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

            CLPlacemark *placemark = placemarks.lastObject;

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

        }];

    });

}




0 0
原创粉丝点击