// 开启地图定位记住在info.plist文件添加下面配置
</array>
<key>NSLocationWhenInUseUsageDescription</key>
<string>是否开启定位</string>
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic,strong) CLLocationManager *mgr;
@end
@implementation ViewController
- (void)viewDidLoad {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
[self.mgr requestWhenInUseAuthorization];
//or [self.locationManage requestAlwaysAuthorization]
}
_mgr.delegate = self;
[_mgr startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
NSLog(@"%s",__func__);
CLLocation *currLocation = [locations lastObject];
NSString *latitude = [NSString stringWithFormat:@"纬度:%3.5f",currLocation.coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"经度:%3.5f",currLocation.coordinate.longitude];
NSString *altitude = [NSString stringWithFormat:@"高度值:%3.5f",currLocation.altitude];
NSLog(@"%@,%@,%@,",latitude,longitude,altitude);
}
- (CLLocationManager *)mgr {
if (!_mgr) {
_mgr = [[CLLocationManager alloc] init];
}
return _mgr;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"%@",error);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end