iOS 自带定位CLLocationManager获取经纬度以及城市名称,并判断是否打开定位权限

97 篇文章 0 订阅
79 篇文章 2 订阅

第一,我们要导入苹果自带的库

#import <MapKit/MapKit.h>

第二,我们要创建对象

@property (strong,nonatomic) CLLocationManager* locationManager;

第三,我们要遵守协议

@interface LocationViewController :UIViewController<CLLocationManagerDelegate>

第四,我们要配置Info.Plist,添加一下两个字段,

Privacy - Location When In Use Usage Description   后面为字符串类型(随意填写) 例如:请您允许,我们将会为您提供更精确的信息

Privacy - Location Always Usage Description        后面为字符串类型(随意填写)例如:请您允许,我们将会为您提供更精确的信息 // 特别声明一下,这个字段的添加要看你的需求,这个功能是指在后台时也可以时时定位,所以app如果不需要的话,不建议添加,否则上线审核会出现问题,如果需要需要做一些声明描述:GPS在后台持续运行,可以大大降低电池的寿命。

效果图如下:

第五,接下来就是大家最喜欢的粘代码

//开始定位
-(void)startLocation{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = 100.0f;
    if ([[[UIDevice currentDevice]systemVersion]doubleValue] >8.0){
        [self.locationManager requestWhenInUseAuthorization];
    }
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        _locationManager.allowsBackgroundLocationUpdates =YES;
    }
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    switch (status) { 
        case kCLAuthorizationStatusNotDetermined:
            if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
                [self.locationManager requestWhenInUseAuthorization];
            }
        break;
        default:
        break;
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *newLocation = locations[0];
    CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
    NSLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude);
    [manager stopUpdatingLocation];
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> *_Nullable placemarks, NSError * _Nullable error) {
        for (CLPlacemark *placein placemarks) {
            NSLog(@"name,%@",place.name);                      // 位置名           
            NSLog(@"thoroughfare,%@",place.thoroughfare);      // 街道            
            NSLog(@"subThoroughfare,%@",place.subThoroughfare);// 子街道           
            NSLog(@"locality,%@",place.locality);              // 市                          NSLog(@"subLocality,%@",place.subLocality);        // 区            
            NSLog(@"country,%@",place.country);                // 国家
            if ([JudgeIDAndBankCardisEmptyOrNull:_gpsCityName]) {
                _gpsCityName=@"定位失败";
            }
        }
    }];

    

}

这样我们就能定位到当前所在城市以及经纬度,在上面的方法可以获取

第六,如果还是获取不到,看看你是不是在模拟器上运行的

第七,如果还是没有解决按照以下图片进行操作到 Capabilities  ——>找到Background Modes 将off改成on 并且勾选 Location updates

这样我们就能定位到当前所在城市以及经纬度,在上面的方法可以获取

 
 
 
 
 
 
 

到此获取经纬度以及城市名称就完成了

 

 

既然用到了定位,那么就有可能用到判断手机给了APP定位权限问题,当然判断着个也要引库和遵守协议

#import <CoreLocation/CLLocationManager.h>



CLLocationManagerDelegate

下面就是判断是否打开了定位权限。返回YES代表没打开,我们需要提使用用户


+ (BOOL)openLocationServiceWithBlock {
    if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) {
        return NO;
    } else {
        return YES;
    }
}
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"alert_title", @"提示") message:NSLocalizedString(@"Opne_location_serive", @"定位服务未开启,请进入系统[设置]> [隐私] > [定位服务]中打开开关,并允许使用定位服务。") delegate:self cancelButtonTitle:NSLocalizedString(@"cancel", @"取消") otherButtonTitles:NSLocalizedString(@"SetUp", @"设置"), nil];
[alertView show];

点击确定按钮我们自动跳转到设置页面去设置即可


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ( buttonIndex == 1 ) {
        NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:settingsURL];
    }
}

 

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王 哪跑!!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值