您的位置:首页 > 移动开发 > IOS开发

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

2014-11-05 12:11 309 查看
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]; //使用中授权

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: