自定义地图导航(Obj-C)

演示代码:

#import "ViewController.h"
#import 

@interface ViewController ()

// 目标地址
@property (weak, nonatomic) IBOutlet UITextField *destination_TF;
// 自定义地图
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
// 位置管理者
@property (nonatomic,strong) CLLocationManager *manager;
@end

@implementation ViewController

// 导航按钮点击事件
- (IBAction)navigationBtnClick:(id)sender {
    
    // 1.创建导航请求
    MKDirectionsRequest *directionsRequest = [[MKDirectionsRequest alloc]init];
    // 2.设置起点和终点
    directionsRequest.source = [MKMapItem mapItemForCurrentLocation];// 起点
    
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    [geocoder geocodeAddressString:self.destination_TF.text completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
        
        
        CLPlacemark *clPlacemark = placemarks.lastObject;
        MKPlacemark *mkPlacemark = [[MKPlacemark alloc]initWithPlacemark:clPlacemark];
        directionsRequest.destination = [[MKMapItem alloc]initWithPlacemark:mkPlacemark];// 终点
        
        // 3.创建导航对象
        MKDirections *directions = [[MKDirections alloc]initWithRequest:directionsRequest];
        
        
        // 4.传递路线并计算
        [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
            
            // 取出路线对象
            for (MKRoute *route in response.routes) {
                
                // 取出路线对象中的具体每一步路线
                for (MKRouteStep *step in route.steps) {
                    
                    NSLog(@"%@",step.instructions);
                }
                
            }
            
        }];
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.manager = [[CLLocationManager alloc]init];
    
    if ([self.manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        
        [self.manager requestWhenInUseAuthorization];
    }
    
    
}

打印结果:

2016-07-11 20:22:43.766 02-自定义地图导航[38600:409617] Start on the route
2016-07-11 20:22:43.767 02-自定义地图导航[38600:409617] Turn left onto 翠微路
2016-07-11 20:22:43.767 02-自定义地图导航[38600:409617] Make a U-turn
2016-07-11 20:22:43.767 02-自定义地图导航[38600:409617] Keep left to merge onto 莲花池西路
2016-07-11 20:22:43.767 02-自定义地图导航[38600:409617] Keep right to merge onto 西二环 toward 广安门桥, 北京南站
2016-07-11 20:22:43.768 02-自定义地图导航[38600:409617] Keep left to merge onto 南二环 toward 陶然桥, 开阳桥
2016-07-11 20:22:43.768 02-自定义地图导航[38600:409617] Take exit toward 华威南路, 东三环
2016-07-11 20:22:43.768 02-自定义地图导航[38600:409617] Turn right onto 华威南路 toward 东三环, 华威桥
2016-07-11 20:22:43.768 02-自定义地图导航[38600:409617] Take exit toward 东五环, 京津高速
2016-07-11 20:22:43.768 02-自定义地图导航[38600:409617] Take exit to merge onto S30京津高速 toward 天津
2016-07-11 20:22:43.768 02-自定义地图导航[38600:409617] Take exit toward G2, 静海
2016-07-11 20:22:43.768 02-自定义地图导航[38600:409617] Take exit toward 天津中心城区, 滨海新区
2016-07-11 20:22:43.768 02-自定义地图导航[38600:409617] Take exit toward G18, 团泊新城
2016-07-11 20:22:43.769 02-自定义地图导航[38600:409617] Keep left toward 临沂
2016-07-11 20:22:43.806 02-自定义地图导航[38600:409617] Keep left to merge onto G2京沪高速
2016-07-11 20:22:43.806 02-自定义地图导航[38600:409617] Take exit toward S49, 宿迁
2016-07-11 20:22:43.806 02-自定义地图导航[38600:409617] Keep left toward S49, 泗洪
2016-07-11 20:22:43.806 02-自定义地图导航[38600:409617] Keep right toward G25, 南京
2016-07-11 20:22:43.806 02-自定义地图导航[38600:409617] Merge onto G42沪蓉高速 toward G2501, G42
2016-07-11 20:22:43.807 02-自定义地图导航[38600:409617] Take exit toward G4211, 板桥汽渡
2016-07-11 20:22:43.807 02-自定义地图导航[38600:409617] Keep left toward G4211, 芜湖
2016-07-11 20:22:43.807 02-自定义地图导航[38600:409617] Take exit toward 三山, 峨桥
2016-07-11 20:22:43.807 02-自定义地图导航[38600:409617] Turn right toward 峨桥
2016-07-11 20:22:43.807 02-自定义地图导航[38600:409617] Take a slight right turn onto 045县道
2016-07-11 20:22:43.807 02-自定义地图导航[38600:409617] Turn left onto 浮峨路 toward 峨山, 浮山
2016-07-11 20:22:43.807 02-自定义地图导航[38600:409617] Turn right
2016-07-11 20:22:43.807 02-自定义地图导航[38600:409617] Take a slight right turn
2016-07-11 20:22:43.807 02-自定义地图导航[38600:409617] Turn right
2016-07-11 20:22:43.808 02-自定义地图导航[38600:409617] Prepare to park your car

根据属性得到的信设置UI数据即可

这里步骤显示为英文,上线应用的时候会选择主语言,设置中文就可以了,如果测试想要使用中文,可以设置plist

en.png
自定义地图导航(Obj-C)_第1张图片
china.png

就显示为中文了:

2016-07-11 20:25:51.346 02-自定义地图导航[38615:412693] 开始路线
2016-07-11 20:25:51.347 02-自定义地图导航[38615:412693] 左转进入翠微路
2016-07-11 20:25:51.347 02-自定义地图导航[38615:412693] 调头
2016-07-11 20:25:51.347 02-自定义地图导航[38615:412693] 靠左行驶并线进入莲花池西路
2016-07-11 20:25:51.347 02-自定义地图导航[38615:412693] 靠右行驶并线进入西二环 ,朝广安门桥和北京南站方向行驶
2016-07-11 20:25:51.347 02-自定义地图导航[38615:412693] 靠左行驶并线进入南二环 ,朝陶然桥和开阳桥方向行驶
2016-07-11 20:25:51.347 02-自定义地图导航[38615:412693] 驶出出口 ,朝华威南路和东三环方向行驶
2016-07-11 20:25:51.348 02-自定义地图导航[38615:412693] 右转进入华威南路 ,朝东三环和华威桥方向行驶
2016-07-11 20:25:51.348 02-自定义地图导航[38615:412693] 驶出出口 ,朝东五环和京津高速方向行驶
2016-07-11 20:25:51.348 02-自定义地图导航[38615:412693] 驶出出口并线进入S30京津高速 ,朝天津方向行驶
2016-07-11 20:25:51.348 02-自定义地图导航[38615:412693] 驶出出口 ,朝G2和静海方向行驶
2016-07-11 20:25:51.348 02-自定义地图导航[38615:412693] 驶出出口 ,朝天津中心城区和滨海新区方向行驶
2016-07-11 20:25:51.348 02-自定义地图导航[38615:412693] 驶出出口 ,朝G18和团泊新城方向行驶
2016-07-11 20:25:51.348 02-自定义地图导航[38615:412693] 靠左行驶 ,朝临沂方向行驶
2016-07-11 20:25:51.348 02-自定义地图导航[38615:412693] 靠左行驶并线进入G2京沪高速
2016-07-11 20:25:51.349 02-自定义地图导航[38615:412693] 驶出出口 ,朝S49和宿迁方向行驶
2016-07-11 20:25:51.349 02-自定义地图导航[38615:412693] 靠左行驶 ,朝S49和泗洪方向行驶
2016-07-11 20:25:51.349 02-自定义地图导航[38615:412693] 靠右行驶 ,朝G25和南京方向行驶
2016-07-11 20:25:51.349 02-自定义地图导航[38615:412693] 并线进入G42沪蓉高速 ,朝G2501和G42方向行驶
2016-07-11 20:25:51.349 02-自定义地图导航[38615:412693] 驶出出口 ,朝G4211和板桥汽渡方向行驶
2016-07-11 20:25:51.349 02-自定义地图导航[38615:412693] 靠左行驶 ,朝G4211和芜湖方向行驶
2016-07-11 20:25:51.349 02-自定义地图导航[38615:412693] 驶出出口 ,朝三山和峨桥方向行驶
2016-07-11 20:25:51.349 02-自定义地图导航[38615:412693] 右转 ,朝峨桥方向行驶
2016-07-11 20:25:51.349 02-自定义地图导航[38615:412693] 稍向右转进入045县道
2016-07-11 20:25:51.350 02-自定义地图导航[38615:412693] 左转进入浮峨路 ,朝峨山和浮山方向行驶
2016-07-11 20:25:51.350 02-自定义地图导航[38615:412693] 右转
2016-07-11 20:25:51.350 02-自定义地图导航[38615:412693] 稍向右转
2016-07-11 20:25:51.350 02-自定义地图导航[38615:412693] 右转
2016-07-11 20:25:51.350 02-自定义地图导航[38615:412693] 准备停车

MKMapItem 地图项目属性介绍:

 @property (nonatomic, readonly) NSString *name; 主要道路
 @property (nonatomic, readonly) NSArray *advisoryNotices; 建议
 @property (nonatomic, readonly) CLLocationDistance distance; 距离
 @property (nonatomic, readonly) NSTimeInterval expectedTravelTime;  预计行驶时间
 @property (nonatomic, readonly) MKDirectionsTransportType transportType; 交通方式
 @property (nonatomic, readonly) MKPolyline *polyline; 折线,路线在地图上展示的线
 @property (nonatomic, readonly) NSArray *steps; 具体的每一步路线

你可能感兴趣的:(自定义地图导航(Obj-C))