iOS:CoreLocation实现定位当前城市

作者: Sunxb | 来源:发表于2016-05-09 15:01 被阅读4655次

首先导入头文件

#import <CoreLocation/CoreLocation.h>

在info.plist文件中添加:

注:NSLocationAlwaysUsageDescription可以不添加

下面是具体用法的demo:

注意:
1.[CLLocationManager locationServicesEnabled]判断定位是否开启是判断的整个手机系统的定位是否打开,并不是针对这一应用.
2.具体在本应用中的是否已经授权定位要通过代理方法判断
如果定位失败(未授权)则会执行此代理方法

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {

}
具体代码:
@interface ViewController ()<CLLocationManagerDelegate>
{
    CLLocationManager * locationManager;
    NSString * currentCity; //当前城市
}
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self locate]; 
}

- (void)locate { 
     //判断定位功能是否打开
    if ([CLLocationManager locationServicesEnabled]) {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
//        [locationManager requestAlwaysAuthorization];
        currentCity = [[NSString alloc] init];
        [locationManager startUpdatingLocation];    
    }
    
}

#pragma mark CoreLocation delegate  

//定位失败则执行此代理方法
//定位失败弹出提示框,点击"打开定位"按钮,会打开系统的设置,提示打开定位服务
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"允许\"定位\"提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //打开定位设置
        NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:settingsURL];
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:cancel];
    [alertVC addAction:ok];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}
//定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    [locationManager stopUpdatingLocation];
    CLLocation *currentLocation = [locations lastObject];
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    
      //反编码
    [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {     
        if (placemarks.count > 0) {
            CLPlacemark *placeMark = placemarks[0];
            currentCity = placeMark.locality;  
            if (!currentCity) {
                currentCity = @"无法定位当前城市";
            } 
            NSLog(@"%@",currentCity); //这就是当前的城市
            NSLog(@"%@",placeMark.name);//具体地址:  xx市xx区xx街道
        }
        else if (error == nil && placemarks.count == 0) {
            NSLog(@"No location and error return");
        }
        else if (error) {
            NSLog(@"location error: %@ ",error);
        }
  
    }];     
}
@end

相关文章

  • iOS:CoreLocation实现定位当前城市

    首先导入头文件 在info.plist文件中添加: 注:NSLocationAlwaysUsageDescript...

  • iOS中的定位功能

    iOS中的定位功能 CoreLocation框架(CoreLocation.framework)可用于定位设备当前...

  • iOS位置服务权限相关

    整个iOS系统的定位服务是否开启 #import 当前...

  • CLLocationManager定位和ibeacon的检测

    一.iOS自带的定位 iOS通过自带的CoreLocation 框架可以获取用户的当前位置,城市,经纬度等信息。 ...

  • iOS CoreLocation 定位实现

    CoreLocation 定位 iOS 地图 反编码 CoreLocation 实现基于 8.0 之后的使用方法,...

  • 定位

    定位 1.实现定位功能 在iOS中使用定位功能,需要导入CoreLocation.h文件,其实现定位功能的步骤如下...

  • 关于地图定位

    CoreLocation框架 一. iOS8.0之前的定位(✨✨✨✨✨) 前台定位导入CoreLocation框架...

  • ios 定位的一些属性

    1、获取当前定位 iOS提供了一个叫作CoreLocation.framework的框架。使用他可以取到自己的定位...

  • iOS14开发-定位与地图

    定位 CoreLocation 是 iOS 中用于设备定位的框架。通过这个框架可以实现定位进而获取位置信息如经度、...

  • CoreLocation框架

    CoreLocation框架 一. iOS8.0之前的定位 1. 前台定位 导入CoreLocation框架以及对...

网友评论

  • Givenbmli:楼主方便的话请上传完整demo
    Givenbmli:@Sunxb 好的谢谢
    Sunxb:@Givenbmli 很久之前写的,demo估计没了,文章里面附的代码也可以的

本文标题:iOS:CoreLocation实现定位当前城市

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