第一步:导入头文件
#import <CoreLocation/CoreLocation.h> #import <CoreLocation/CLLocationManagerDelegate.h>
第二步:设置代理
CLLocationManagerDelegate
第三步:创建一个属性
@property(nonatomic, strong) CLLocationManager *locationManager;
第四步:初始化
//定位服务管理对象初始化 _locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; _locationManager.desiredAccuracy = kCLLocationAccuracyBest; _locationManager.distanceFilter = 100.f; // 设定最少移动1000米才能刷新
第五步:实现代理方法
#pragma mark - 实现位置刷新 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *currLocation = [locations lastObject]; NSLog(@"经度= %f 纬度= %f 高度= %f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude); }
第六步:开始定位
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [_locationManager startUpdatingLocation]; // 开始定位 }
第七步:结束定位
- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [_locationManager stopUpdatingLocation]; // 停止定位 }
时间: 2024-12-25 13:40:10