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]; //使用中授权
}