#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.第一步
// 在Info.plist文件中添加如下两个字段中的至少一个
// NSLocationWhenInUseUsageDescription
// NSLocationAlwaysUsageDescription
// 如果两个都设置,取NSLocationAlwaysUsageDescription的描述(描述可空)
// 开始定位
[self.locationManager startUpdatingLocation];
}
- (CLLocationManager *)locationManager {
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
[_locationManager requestAlwaysAuthorization]; // 2.必须设置
_locationManager.delegate = self;
}
return _locationManager;
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
CLLocation *location = [locations firstObject];
NSLog(@"latitude = %f, longitude = %f", location.coordinate.latitude, location.coordinate.longitude);
}
@end
苹果地图使用之定位
最新推荐文章于 2021-12-30 15:10:33 发布