if ([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest; //控制定位精度,越高耗电量越大。
_locationManager.distanceFilter = 100; //控制定位服务更新频率。单位是“米”
[_locationManager startUpdatingLocation];
//在ios 8.0下要授权
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[_locationManager requestWhenInUseAuthorization]; //调用了这句,就会弹出允许框了.
}
注意:
在Info.plist文件还要加上NSLocationWhenInUseUsageDescription这个key,Value可以为空,
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation * currLocation = [locations lastObject];
NSLog(@"%@",[NSString stringWithFormat:@"%.3f",currLocation.coordinate.latitude]);
NSLog(@"%@",[NSString stringWithFormat:@"%.3f",currLocation.coordinate.longitude]);
}