首先导入
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
添加请求权限的key
<key>NSLocationWhenInUseUsageDescription</key>
<string>YES</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>YES</string>
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];
实现代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { float altitude = newLocation.altitude; NSLog(@"海拔高度为:%.2fm",altitude); NSLog(@"垂直精度为:%.2fm",newLocation.verticalAccuracy); }
出现错误
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ NSLog(@"%@",error); }
此文章只做学习记录