iOS. 原生获取当前经度 纬度。 地址




#import <CoreLocation/CoreLocation.h>


<CLLocationManagerDelegate>



/**

 获取当前位置

 */

- (void)locationButton{

    NSLog(@"=====获取当前位置");

    

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

    self.locationManager.delegate = self;

    self.locationManager.distanceFilter = 5.0;

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    

    if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])

    {

        [self.locationManager requestAlwaysAuthorization]; // 永久授权

        [self.locationManager requestWhenInUseAuthorization]; //使用中授权

    }

    [self.locationManager startUpdatingLocation];

    

}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

{

    

    // 获取当前所在的城市名

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

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

    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error){

        if (array.count > 0){

            CLPlacemark *placemark = [array objectAtIndex:0];

            NSLog(@"newLocation:%.2f=====%.2f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);

            //获取城市

            NSString *city = placemark.locality;

            if (!city) {

                //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)

                city = placemark.administrativeArea;

            }

 NSLog(@"++++%@===%@",newLocation.coordinate.latitude,newLocation.coordinate.longitude);

            NSLog(@"city = %@", city);

            NSLog(@"--%@",placemark.name);

            NSLog(@"++++%@",placemark.subLocality);

            NSLog(@"country == %@",placemark.country);//中国

            NSLog(@"administrativeArea == %@%@%@%@",placemark.administrativeArea,placemark.administrativeArea,placemark.subLocality,placemark.name); //河北省

        }

        else if (error == nil && [array count] == 0)

        {

            NSLog(@"No results were returned.");

        }

        else if (error != nil)

        {

            NSLog(@"An error occurred = %@", error);

        }

    }];

    

    [self.locationManager stopUpdatingLocation];

}


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

{

    //设置提醒

    

    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"允许\"定位\"提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        //打开定位设置

        NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

        [[UIApplication sharedApplication] openURL:settingURL];

        

       

    }];

    

    UIAlertAction * canct = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        

    }];

    

    [alert addAction:canct];

    [alert addAction:ok];

   [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];

    

}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值