利用CLLocationManager获取当前位置坐标时,
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// 设置定位精度
// kCLLocationAccuracyNearestTenMeters:精度10米
// kCLLocationAccuracyHundredMeters:精度100 米
// kCLLocationAccuracyKilometer:精度1000 米
// kCLLocationAccuracyThreeKilometers:精度3000米
// kCLLocationAccuracyBest:设备使用电池供电时候最高的精度
// kCLLocationAccuracyBestForNavigation:导航情况下最高精度,一般要有外接电源时才能使用
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序
// 它的单位是米,这里设置为至少移动1000再通知委托处理更新;
_locationManager.distanceFilter = 5.0f; // 如果设为kCLDistanceFilterNone,则每秒更新一次;
[_locationManager requestAlwaysAuthorization];//添加这句
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)gps111:(id)sender {
// 判断的手机的定位功能是否开启
// 开启定位:设置 > 隐私 > 位置 > 定位服务
if([CLLocationManager locationServicesEnabled]) {
// 启动位置更新
// 开启位置更新需要与服务器进行轮询所以会比较耗电,在不需要时用stopUpdatingLocation方法关闭;
[self.locationManager
startUpdatingLocation];
}
else{
NSLog(@"请开启定位功能!");
}
}
程序不会执行代理方法获取当前位置
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
self.textLabel.text = [NSString stringWithFormat:@"%@-%f | %@-%f", @"经度:", newLocation.coordinate.longitude, @"纬度:", newLocation.coordinate.latitude];
[manager stopUpdatingLocation];
[self getCurrentCityWithNewLocation:newLocation];
}
// 定位失误时触发
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"error:%@",error);
}
此时应在info.plist配置中加入NSLocationAlwaysUsageDescription, NSLocationWhenInUseUsageDescription字段