美文网首页
ios 定位当前位置

ios 定位当前位置

作者: 每天刷两次牙 | 来源:发表于2017-05-24 16:15 被阅读381次

    iOS定位当前城市

    1、首先在plist表里边添加Privacy - Location Usage Description和NSLocationWhenInUseUsageDescription(这个是在程序使用期间进行定位,如果需要一直获取,添加NSLocationAlwaysUsageDescription),都为String类型

    2、其次在.m中引入CoreLocation/CoreLocation.h头文件,并且遵循CLLocationManagerDelegate代理。然后定义一个CLLocationManager对象,代码如下:

    #import"CoreLocation/CoreLocation.h"

    @interfaceViewController () 

    @property (strong, nonatomic) CLLocationManager*locationManager;

    @end

    if ([CLLocationManager locationServicesEnabled]) {//判断定位操作是否被允许

    self.locationManager = [[CLLocationManager alloc] init];

    self.locationManager.delegate = self;//遵循代理

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    self.locationManager.distanceFilter = 10.0f;

    [_locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据(iOS8以上版本定位需要)

    [self.locationManager startUpdatingLocation];//开始定位

    }else{//不能定位用户的位置的情况再次进行判断,并给与用户提示

    //1.提醒用户检查当前的网络状况

    //2.提醒用户打开定位开关

    }

    }

    #pragma mark === 定位代理方法

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    //当前所在城市的坐标值

    CLLocation *currLocation = [locations lastObject];

    NSLog(@"经度=%f 纬度=%f 高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);

    //根据经纬度反向地理编译出地址信息

    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];

    [geoCoder reverseGeocodeLocation:currLocation completionHandler:^(NSArray *placemarks, NSError *error) {

    for (CLPlacemark * placemark in placemarks) {

    NSDictionary *address = [placemark addressDictionary];

    //  Country(国家)  State(省)  City(市)

    NSLog(@"#####%@",address);

    NSLog(@"%@", [address objectForKey:@"Country"]);

    NSLog(@"%@", [address objectForKey:@"State"]);

    NSLog(@"%@", [address objectForKey:@"City"]);

    self.cllocationCity =[address objectForKey:@"City"];

    }

    }];

    }

    //定位失败弹出提示框,点击"打开定位"按钮,会打开系统的设置,提示打开定位服务

    - (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];

    }

    相关文章

      网友评论

          本文标题:ios 定位当前位置

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