美文网首页iOS收藏基础知识
ios - 调用系统定位获取当前经纬度与地理信息

ios - 调用系统定位获取当前经纬度与地理信息

作者: 壮士你怎么了 | 来源:发表于2018-10-25 12:04 被阅读1080次

    项目中用到了城市定位,获取所在城市的经纬度以及名称。由于没有用到其他更复杂的地图功能,所以就选择了用系统自带的定位来获取当前经纬度与地理信息。下面直接上代码。

    1.在info.plist中加入以下两个字段
    允许在前台使用时获取GPS的描述

    定位权限:Privacy - Location When In Use Usage Description

    允许永久使用GPS描述

    定位权限: Privacy - Location Always Usage Description

    如下图:


    01.png

    2.加入framework包:CoreLocation.framework如下图:

    2.png

    3.1 在.m中引入CoreLocation/CoreLocation.h头文件,并且遵循CLLocationManagerDelegate代理。然后定义一个CLLocationManager对象,并且声明变量,代码如下:

    
    #import "KYAchievementManagerVC.h"
    
    @interface JBHomeVC ()<CLLocationManagerDelegate>
    
    {
    
        CLLocationManager*locationmanager;//定位服务
    
        NSString*strlatitude;//经度
    
        NSString*strlongitude;//纬度
    
    }
    
    

    3.2 调用方法

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self startLocation];
    }
    

    3.3 开始定位

    #pragma mark - 定位
    //开始定位
    -(void) startLocation
    {
        //判断定位功能是否打开
        if ([CLLocationManager locationServicesEnabled]) {
            locationmanager = [[CLLocationManager alloc]init];
            locationmanager.delegate = self;
            [locationmanager requestAlwaysAuthorization];
            [locationmanager requestWhenInUseAuthorization];
            
            //设置寻址精度
            locationmanager.desiredAccuracy = kCLLocationAccuracyBest;
            locationmanager.distanceFilter = 5.0;
            [locationmanager startUpdatingLocation];
        }
    }
    

    3.4 定位失败后的代理方法

    #pragma mark CoreLocation delegate (定位失败)
    //定位失败后调用此代理方法
    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        //设置提示提醒用户打开定位服务
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"允许定位提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:nil];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:okAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    

    3.5 定位成功后的代理方法

    #pragma mark 定位成功后则执行此代理方法
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
    {
        [locationmanager stopUpdatingHeading];
        //旧址
        CLLocation *currentLocation = [locations lastObject];
        CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
        //打印当前的经度与纬度
        NSLog(@"%f,%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
        
        //反地理编码
        [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error)
        {
            NSLog(@"反地理编码");
            NSLog(@"反地理编码%ld",placemarks.count);
            if (placemarks.count > 0) {
                CLPlacemark *placeMark = placemarks[0];
                self.label_city.text = placeMark.locality;
                if (!self.label_city.text) {
                    self.label_city.text = @"无法定位当前城市";
                }
                /*看需求定义一个全局变量来接收赋值*/
                NSLog(@"城市----%@",placeMark.country);//当前国家
                NSLog(@"城市%@",self.label_city.text);//当前的城市
                NSLog(@"%@",placeMark.subLocality);//当前的位置
                NSLog(@"%@",placeMark.thoroughfare);//当前街道
                NSLog(@"%@",placeMark.name);//具体地址
                
            }
        }];
        
    }
    
    1. 打印数据
    3.png
    1. 修改模拟器经纬度
    4.png
    5.png

    6.修改模拟器语言

    Setting(模拟器中的应用程序)- General - Language & Region - iPhone Language - 简体中文

    相关文章

      网友评论

        本文标题:ios - 调用系统定位获取当前经纬度与地理信息

        本文链接:https://www.haomeiwen.com/subject/mbmqtqtx.html