iOS:定位、地址和经纬度互转

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-28 22:14   24   0

一、使用CLLocationManager进行定位:

1.增加定位权限:

打开Info.plist,在<dict>节点增加NSLocationUsageDescription值:

<dict>
 ...
 <key>NSLocationUsageDescription</key>
 <string>请求获取位置信息</string>
</dict>

2.导入定位相关依赖包,实现CLLocationManagerDelegate,并初始化定位管理器:

//导入定位相关依赖包
#import <CoreLocation/CoreLocation.h>

//实现定位代理(接口)
@interface ViewController () <CLLocationManagerDelegate>
//定位管理器
@property (nonatomic, strong) CLLocationManager *localM;

@end

@implementation ViewController

/*
 懒加载初始化定位管理器
 */
- (CLLocationManager *)clm{
    //如果定位不可用,直接返回
    if(![CLLocationManager locationServicesEnabled]){
        return nil;
    }
    if(_localM == nil){
        //创建定位管理器
        _localM = [[CLLocationManager alloc] init];
        //设置当前类实现定位代理方法
        _localM.delegate = self;
    }
    return _localM;
}

3.重写locationManager-didUpdateLocations方法,接收当前实时更新的位置信息:

/*
 CLLocationManagerDelegate方法:更新定位信息
 */
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    //取出位置信息
    CLLocation *location = [locations firstObject];
    
    //获取当前位置经纬度
    CLLocationCoordinate2D coordinate = location.coordinate;
    NSLog(@"latitude: %f, longitude: %f", coordinate.latitude, coordinate.longitude);
    
    //获取当前位置海拔
    CLLocationDistance altitude = location.altitude;
    NSLog(@"海拔: %f", altitude);
    
    //获取当前航向,取值0.0度-359.9度,0.0为真北方向
    CLLocationDirection course = location.course;
    NSLog(@"航向: %f", course);
    
    //获取当前行走速度
    CLLocationSpeed speed = location.speed;
    NSLog(@"航向: %f", speed);
}

4.开启实时定位:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //开始定位
    [self.localM startUpdatingLocation];
    //停止定位更新
//    [manager stopUpdatingLocation];
}

二、使用CLGeocoder实现地址和经纬度互转:

1.将地址字符串转为经纬度:

/*
 自定义方法:将地址转为经纬度
 */
- (void)adressToCoordinate{
    NSString *adress = @"杭州";
    //编码地址
    [self.geocoder geocodeAddressString:adress completionHandler:^(NSArray<CLPlacemark *> *placemarks, NSError *error) {
        //地址错误
        if(error){
            return;
        }
        //取出首个地址
        CLPlacemark *firstLp = [placemarks firstObject];
        
        /*
         取出地址列表
         */
        for (CLPlacemark *lp in placemarks) {
            //取出位置信息
            CLLocation *location = lp.location;
            //获取位置经纬度
            CLLocationCoordinate2D coordinate = location.coordinate;
            NSLog(@"latitude: %f, longitude: %f, name: %@", coordinate.latitude, coordinate.longitude, lp.name);
            //循环NSDictionary中的地址信息
            [lp.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                NSLog(@"key: %@, value: %@", key, obj);
            }];
        }
    }];
}

2.将经纬度转为地址字符串:

/*
 自定义方法:将经纬度转为地址
 */
- (void)coordinateToAdress{
    //根据经纬度值创建地址对象
    CLLocation *location = [[CLLocation alloc] initWithLatitude:30.28 longitude:120.15];
    //反编码经纬度为地址
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> *placemarks, NSError *error) {
        //经纬度错误
        if(error){
            return;
        }
        //取出首个地址
        CLPlacemark *firstLp = [placemarks firstObject];
        
        /*
         取出地址列表
         */
        for (CLPlacemark *lp in placemarks) {
            //取出位置信息
            CLLocation *location = lp.location;
            //获取位置经纬度
            CLLocationCoordinate2D coordinate = location.coordinate;
            NSLog(@"latitude: %f, longitude: %f, name: %@", coordinate.latitude, coordinate.longitude, lp.name);
            //循环NSDictionary中的地址信息
            [lp.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                NSLog(@"key: %@, value: %@", key, obj);
            }];
        }
    }];
}

三、计算2个位置之间的直线距离:

/*
 自定义方法:计算2个位置之间的直线距离
 */
- (void)getDistance{
    //第1个位置经纬度值
    CLLocation *cl1 = [[CLLocation alloc] initWithLatitude:100 longitude:100];
    //第2个位置经纬度值
    CLLocation *cl2 = [[CLLocation alloc] initWithLatitude:200 longitude:200];
    //计算2个位置之间的直线距离
    CLLocationDistance distance = [cl1 distanceFromLocation:cl2];
    NSLog(@"distance : %f", distance);
}

四、监控某个地址区域:

/*
 自定义方法:开始监控某个地址区域
 */
- (void)monitorLocation{
    CLRegion *region = [[CLRegion alloc] init];
    //开始监控某个区域
    [self.localM startMonitoringForRegion:region];
}
/*
 CLLocationManagerDelegate方法:进入区域
 */
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
    
}
/*
 CLLocationManagerDelegate方法:离开区域
 */
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
    
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP