iOS进阶_地图定位

一、定位步骤

1.Xcode自带地图,直接先引入头文件

#import <CoreLocation/CoreLocation.h>

2.CLLocation框架中的CLLocationManager用于管理定位的管理器

//CLLocation框架中的CLLocationManager用于管理定位的管理器
@property (nonatomic, strong)CLLocationManager *manager;

初始化定位管理器:self.manager = [[CLLocationManager alloc] init];

3(1)进行隐私判断

    if ([CLLocationManager locationServicesEnabled]) {
        NSLog(@"是否前往隐私进行设置允许定位");//此处自己设置提示框
    }

3(2)并授权

    if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 8.0) {       //进行版本的判断
        /*定位服务授权状态,返回枚举类型:
         kCLAuthorizationStatusNotDetermined: 用户尚未做出决定是否启用定位服务
         kCLAuthorizationStatusRestricted: 没有获得用户授权使用定位服务,可能用户没有自己禁止访问授权
         kCLAuthorizationStatusDenied :用户已经明确禁止应用使用定位服务或者当前系统定位服务处于关闭状态
         kCLAuthorizationStatusAuthorizedAlways: 应用获得授权可以一直使用定位服务,即使应用不在使用状态
         kCLAuthorizationStatusAuthorizedWhenInUse: 使用此应用过程中允许访问定位服务
         */
        if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
            //在授权请求之前需要在info.plist文件中设置允许定位的内容:NSLocationWhenInUseUsageDescription,在NSLocationWhenInUseUsageDescription后面的values值上添加提示,该提示会显示在请求授权时弹出的提示框上
            //请求授权
            [self.manager requestWhenInUseAuthorization];
        }
    }

4.设置管理器的代理和相关属性

    self.manager.delegate = self;
    //设置精度
    self.manager.desiredAccuracy = 100;
    //设置最小更新距离
    self.manager.distanceFilter = 100;
    
    //开启定位
    [self.manager startUpdatingLocation]

5.CLLocation框架中的CLGeocoder能进行地理位置的编码和反编码

//CLLocation框架中的CLGeocoder能进行地理位置的编码和反编码
@property (nonatomic, strong) CLGeocoder *geocoder;

6.定位的代理方法

//这个代理方法是定位成功之后开始更新位置信息,只要移动设置的最小距离之后就会自动开始走这个方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    
    //获取最后一次的位置
    CLLocation *location = locations.lastObject;
    //获取位置
    CLLocationCoordinate2D coordinate = location.coordinate;
    
    NSLog(@"经度:%f, 纬度:%f, 海拔:%f, 航向:%f, 行走速度:%f", coordinate.longitude, coordinate.latitude, location.altitude, location.course, location.speed);    //longitude经度,latitude纬度,location.altitude海拔,location.course航海方向,location.speed速度
    
    //为了节省电源,如果不使用定位,需要把定位关掉
    [self.manager stopUpdatingLocation];
    
}

//定位失败方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    
    NSLog(@"定位失败");
}
实现定位

二、根据地名编码获取经纬度输入地名自动联网获取该地名对应的所有位置

#pragma mark - 根据地名编码获取相关信息
- (void)getCoordinateByAddress:(NSString *)address {
    
    //编码方法
    [_geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        /*         addressDictionary中包括的信息有:
         //        NSString *name=placemark.name;//地名
         //        NSString *thoroughfare=placemark.thoroughfare;//街道
         //        NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等
         //        NSString *locality=placemark.locality; // 城市
         //        NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑
         //        NSString *administrativeArea=placemark.administrativeArea; // 州
         //        NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息
         //        NSString *postalCode=placemark.postalCode; //邮编
         //        NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码
         //        NSString *country=placemark.country; //国家
         //        NSString *inlandWater=placemark.inlandWater; //水源、湖泊
         //        NSString *ocean=placemark.ocean; // 海洋
         //        NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标
         */
        //根据返回的地标,取出第一个位置(地标的位置很多)
        CLPlacemark *mark = placemarks.firstObject;
        
        //根据地标得到location
        CLLocation *location = mark.location;
        
        //根据location获取区域
        CLRegion *region = mark.region;
        
        //获取字典信息
        NSDictionary *addressDic = mark.addressDictionary;
        
        NSLog(@"地标:%@, 区域:%@, 字典-地理位置信息(包括街道、地名...)%@",location, region, addressDic);
        
        
    }];
    
}
地名获取经纬度方法

 二、三两个方法不能在一个viewDidLoad里顺序执行,只会执行第一个不执行第二个,或者延时一段时间后,再执行第二个,建议添加两个按钮分别执行这两个方法。

三、根据经纬度反编码取出地名

输入坐标经纬度获取该位置地理信息

- (void)getAddressByLatitude:(CLLocationDegrees)latitude
                   Longitude:(CLLocationDegrees)longitude {
    
    //反编码
    //创建CLLocation
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    
    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        NSDictionary *dic = placemarks.firstObject.addressDictionary;
        NSLog(@"反编码地理位置信息:%@",dic);
        
    }];
    
}
经纬度获取地名方法

四、计算两点之间的距离
输入两个坐标,获得两坐标之间的距离

- (void)distance {
    
    /*
     北京:N40,E116
     大连:N39,E121
     */
    
    //创建位置1
    CLLocation *locationBeiJing = [[CLLocation alloc] initWithLatitude:40 longitude:116];
    //创建位置2
    CLLocation *locationDaLian = [[CLLocation alloc] initWithLatitude:39 longitude:121];
    
    //距离计算方法
    CLLocationDistance distance = [locationBeiJing distanceFromLocation:locationDaLian];
    
    NSLog(@"北京到大连的距离为:%f", distance);
    
}
获取距离方法
原文地址:https://www.cnblogs.com/TWFUQTN/p/5544906.html