苹果地图使用之定位

来源:互联网 发布:火星网络用语 编辑:程序博客网 时间:2024/04/30 03:18
#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

0 0
原创粉丝点击