iOS 在8.0下如何获取当前位置的精度和纬度

iOS 8.0 下,苹果地图定位功能发生了部分改变。要想获取用户的当前位置的经纬度要做到:

1.在你的Supporting Files 中的 info.plist 文件中应该增加以下两项:

– ADD two new keys of Type STRING to the Info.plist of your project:

NSLocationAlwaysUsageDescription

NSLocationWhenInUseUsageDescription

2.在viewDidLoad()方法中

{

_manager = [[CLLocationManageralloc] init];

    _manager.delegate =self;

    _manager.desiredAccuracy =kCLLocationAccuracyBest;

    _manager.distanceFilter =kCLDistanceFilterNone;

    [_managerrequestWhenInUseAuthorization];

    

    if ([CLLocationManagerlocationServicesEnabled]) {

        [_managerstartUpdatingLocation];

        

    }

}

3.实现代理方法,获取用户的最新位置:

#pragma mark - LocationManager Delegate Method

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

{

   // locations 中存放的是用户当前位置的最新更新

    CLLocation *location = [locationslastObject];

    _userlatitude = [NSStringstringWithFormat:@"%f",location.coordinate.latitude];

    _userlongitude = [NSStringstringWithFormat:@"%f",location.coordinate.longitude];

     NSLog(@"地理位置:%@%@",_userlongitude,_userlatitude);

    [_managerstopUpdatingLocation];

}

// 位置更新失败

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

{

    NSLog(@"位置更新error:%@",error);


}

4.在测试时 如果使用的模拟器则要要将位置更改为 中国香港

之后就可以获取用户当前位置的经纬度了。

ios7.0 适配处理

if([_manager respondsToSelector:@selector(requestAlwaysAuthorization)]) {

        

        [_manager requestAlwaysAuthorization]; // 永久授权

        

        [_manager requestWhenInUseAuthorization]; //使用中授权

        

    }



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值