本文主要是介绍地图定位导航,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在info.plist文件中加两个key分别是NSLocationAlwaysUsageDescription,
Privacy - Location Usage Description
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>@interface ViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>
@property(nonatomic, strong)CLLocationManager *locMgr;
@property(nonatomic, strong)MKMapView *mapView;
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.self.view.backgroundColor = [UIColor grayColor];[self.view addSubview:self.mapView];if ([self.locMgr respondsToSelector:@selector(requestAlwaysAuthorization)]) {[self.locMgr requestAlwaysAuthorization];}// 判断用户定位服务是否开启if ([CLLocationManager locationServicesEnabled]) {// 开始定位用户的位置[self.locMgr startUpdatingLocation];// 每隔多少米定位一次self.locMgr.distanceFilter = kCLDistanceFilterNone; // 这里设置为任何移动都会进行定位// 设置定位的精度, 一般精准度越高, 越耗电(这里设置为精准度最高的, 适用于导航应用)self.locMgr.desiredAccuracy = kCLLocationAccuracyBestForNavigation;NSLog(@"已定位");} else {NSLog(@"未授权定位");// 不能定位用户的位置// 1. 提醒用户检查当前的网络状态// 2. 提醒用户打开定位开关UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"无法定位" message:@"请检查定位服务是否打开或网络服务是否良好" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];[alertView addAction:okAction];[self presentViewController:alertView animated:YES completion:nil];}// 测试方法, 计算两个位置之间的距离[self countDistance];// 打开系统地图进行导航定位CLLocationCoordinate2D startCoor = self.mapView.userLocation.coordinate;CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude + 0.01, startCoor.longitude + 0.01);MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];toLocation.name = @"to name";[MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];}- (MKMapView *)mapView {if (!_mapView) {self.mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];self.mapView.mapType = MKMapTypeSatellite;self.mapView.delegate = self;}return _mapView;
}#pragma mark - CLLocationManagerDelegate
// 当定位到用户位置时, 就会调用(调用频率比较频繁)
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {NSLog(@"dfasdfa");// locations数组里存放的是CLLocation对象, 一个CLLocation对象就代表着一个位置CLLocation *loc = [locations firstObject];// 纬度:// 经度:NSLog(@"纬度 = %f,经度 = %f", loc.coordinate.latitude, loc.coordinate.longitude);NSLog(@"%ld", locations.count);// 停止更新位置 (如果定位服务不需要实时更新的话, 那么应该停止位置的更新)// [self.locMgr stopUpdatingLocation];
}- (void)countDistance {// 根据经纬度创建两个位置对象CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:40 longitude:116];CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:41 longitude:116];// 计算两个位置之间的距离CLLocationDistance distance = [loc1 distanceFromLocation:loc2];NSLog(@"(%@) 和 (%@) 的距离 = %fM", loc1, loc2, distance);
}
#pragma mark - 懒加载
- (CLLocationManager *)locMgr {if (!_locMgr) {// 1. 创建位置管理器self.locMgr = [[CLLocationManager alloc] init];// 2. 设置代理_locMgr.delegate = self;}return _locMgr;
}
这篇关于地图定位导航的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!