iOS - 原生地图定位

1,plist文件配置

必须包含NSLocationAlwaysAndWhenInUseUsageDescription和NSLocationWhenInUseUsageDescription键,并使用字符串值向用户解释应用程序如何使用这些数据。
对应的授权方法为

//始终授权
[_locationM requestAlwaysAuthorization];
//前台授权
[_locationM requestWhenInUseAuthorization];

1,懒加载地图管理者CLLocationManager


-(CLLocationManager *)locationManager
{
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc]init];
        // 设置定位距离过滤参数 (当上次定位和本次定位之间的距离 > 此值时,才会调用代理通知开发者)
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        // 设置定位精度 (精确度越高,越耗电,所以需要我们根据实际情况,设定对应的精度)
        //允许后台获取信息
        _locationManager.allowsBackgroundLocationUpdates = YES;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.delegate = self;
    }
    return _locationManager;
}

2,相关属性

//每隔多少米定位一次
@property(assign, nonatomic) CLLocationDistance distanceFilter;
//定位精确度(越精确就越耗电)
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;

3,常用方法

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

当调用了startUpdatingLocation方法后,就开始不断地定位用户的位置,中途会频繁地调用代理的下面方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
locations参数里面装着CLLocation对象

4,位置属性CLLocation

用来表示某个位置的地理信息,比如经纬度、海拔等等

//经纬度
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;
//海拔
@property(readonly, nonatomic) CLLocationDistance altitude;
//路线,航向(取值范围是0.0° ~ 359.9°,0.0°代表真北方向)
@property(readonly, nonatomic) CLLocationDirection course;
//行走速度(单位是m/s)
@property(readonly, nonatomic) CLLocationSpeed speed;
//时间戳
@property(readonly, nonatomic, copy) NSDate *timestamp;
//水平精度
@property(readonly, nonatomic) CLLocationAccuracy horizontalAccuracy;
//垂直精度
@property(readonly, nonatomic) CLLocationAccuracy verticalAccuracy;

用下面方法可以计算2个位置之间的距离
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location

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

typedef struct {
        CLLocationDegrees latitude; // 纬度
        CLLocationDegrees longitude; // 经度
} CLLocationCoordinate2D;

5,前台定位授权

//请求前后台定位授权
 [_locationManager requestAlwaysAuthorization];
//    请求前台授权
//    [_locationManager requestWhenInUseAuthorization];
//频繁的定位
 [self.locationManager startUpdatingLocation];
// 只请求一次定位位置(注意:必须实现定位失败的代理方法-locationManager:didFailWithError:方法)
        // 不能与startUpdatingLocation方法同时使用
      //  [self.locationM requestLocation];
    

    
    // 2.判断定位服务是否可用
    if([CLLocationManager locationServicesEnabled])
    {
        [self.locationManager startUpdatingLocation];
    }else
    {
        NSLog(@"不能定位");
    }

6,后台定位授权

在前台定位授权的基础上,勾选了后台模式(capabilities的Background modes)location updates之后, 还需要额外设置属性allowsBackgroundLocationUpdates = YES;

7,若没有授权,跳转到授权设置界面

 NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if([[UIApplication sharedApplication] canOpenURL:settingURL])
    {
        [[UIApplication sharedApplication] openURL:settingURL options:@{} completionHandler:nil];
    }

8,定位到的代理方法

-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
{
      CLLocation *location = locations.lastObject;
    NSLog(@"位置信息维度%f,经度%f,海拔%f,方向%f,速度%fM/s,时间%@",location.coordinate.latitude,location.coordinate.longitude,location.altitude,location.course,location.speed,location.timestamp);
}

9,方法

//计算两个位置的距离
- (CLLocationDistance)distanceFromLocation:(CLLocation *)location

10,注意点

1,使用前先判断位置是否有效

 if (location.horizontalAccuracy < 0) return;

2,定位非常耗电,在需要时开启,拿到定位不需要以后一定要关闭定位

11,地理编码(CLGeocoder)和反地理编码(CLPlacemark )

地理编码:指根据地址关键字, 将其转换成为对应的经纬度等信息
反地理编码: 指根据经纬度信息, 将其转换成为对应的省市区街道等信息

//地理编码头文件
#import <AddressBook/AddressBook.h>
//对象
// 用作地理编码、反地理编码的工具类
@property (nonatomic, strong) CLGeocoder *geoC;
//地理编码
    [self.geoC geocodeAddressString:self.cityField.text completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {
        // 包含区,街道等信息的地标对象
        CLPlacemark *placemark = [placemarks firstObject];
        // 城市名称
        //        NSString *city = placemark.locality;
        // 街道名称
        //        NSString *street = placemark.thoroughfare;
        // 全称
        NSString *name = placemark.name;
        self.cityField.text = [NSString stringWithFormat:@"%@", name];
        self.latitude.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.latitude];
        self.longitude.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.longitude];
    }];
//反地理编码
 // 创建CLLocation对象
    CLLocation *location = [[CLLocation alloc] initWithLatitude:[self.latitude.text doubleValue] longitude:[self.longitude.text doubleValue]];
    // 根据CLLocation对象进行反地理编码
    [self.geoC reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {
        // 包含区,街道等信息的地标对象
        CLPlacemark *placemark = [placemarks firstObject];
        // 城市名称
        //        NSString *city = placemark.locality;
        // 街道名称
        //        NSString *street = placemark.thoroughfare;
        // 全称
        NSString *name = placemark.name;
        self.cityField.text = [NSString stringWithFormat:@"%@", name];
    }];

附上demo的GitHub地址https://github.com/CDLOG/locationDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容