年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 5371|回复: 0

百度地图第七讲:路径规划

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-11-20 18:17:44 | 显示全部楼层 |阅读模式
    一、先看效果图:
    iOS-模拟器屏幕快照“2014年11月20日-下午5.42.39”.jpg

    二、实现步骤
    1、创建基本视图(这一步可先跳过,直接在代码中写入需要传入的数据即可)
    2、实现定位功能,这不是该Demo的核心,甚至可以去掉,具体做法可参照http://www.yusian.com/bbs/thread-8375-1-1.html
    3、如何计算两地的路径并在地图上展现出来(重点)
    3.1、创建两个点(BMKPlanNode类,如果是步行创建后给namecityName赋值即可)
    3.2、路径计算选项(步行、公交、自驾等),发起搜索
    3.3、在代理方法中获取网络计算结果
    3.4、处理计算结果(复杂的来了,做好准备!!)
    3.4.1、如果是步行计算结果返回的是一个BMKWalkingRouteResult对象
    3.4.2、该对象中有个结果为BMKWalkingRouteLine类型的数组,因为有可能会有多条线路
    3.4.3、我们取第一条线路(数组中的第一个元素,一般最优的放在最前面)
    3.4.4、该对象中有个结果为BMKWalkingStep类型的数组,一条线路中分为多个路段(因为会有岔路嘛)
    3.4.5、我们取出所有的路段,每个路段都会有个入口信息与出口信息BMKRouteNode类型数据,这个可做为路径点标注使用
    3.4.6、每个路段信息中包含了该路段的地理坐标集合:BMKMapPoint类型数据的组数,我们最终的目的就是为了取出这个数组中的所有元素;
    3.4.7、BMKMapPoint数组是一个c语言数组,不能直接用oc的语法去处理,之所有要取出所有的这种类型数据是因为我们最终的路径是由这些点组成,然后再用折线将这些点连接成线!
    3.4.8、小结一下,关键性的三个数组:路线--路段--路径点(BMKWalkingRouteLine--BMKWalkingStep--BMKMapPoint
    3.5、获取到所有的路径点之后,将这个数组传给绘图类BMKPolyline,让其绘出折线,其实所谓的路径曲线就是由很多条小段的折线组成。
    3.6、通过mapView的代理方法,将折线展示到地图上,路径规划大功造成!

    三、关键性代码示例
    1. #import "SAViewController.h"
    2. #import "BMapKit.h"
    3. @interface SAViewController () <BMKRouteSearchDelegate, BMKMapViewDelegate, BMKLocationServiceDelegate>
    4. @property (nonatomic, strong) BMKLocationService *location;     // 定位服务
    5. @property (nonatomic, strong) BMKRouteSearch     *routeSearch;  // 路径搜索
    6. @property (nonatomic, strong) BMKMapView         *mapView;      // 地图
    7. @property (weak, nonatomic) IBOutlet UITextField *start;        // 出发地
    8. @property (weak, nonatomic) IBOutlet UITextField *end;          // 目的地
    9. // 开始搜索
    10. - (IBAction)search;
    11. @end
    12. @implementation SAViewController
    13. - (void)viewDidLoad
    14. {
    15.     [super viewDidLoad];
    16.     // 添加地图视图
    17.     self.mapView.frame = self.view.frame;
    18.     [self.view insertSubview:self.mapView atIndex:0];
    19.    
    20.     // 开启定位服务
    21.     self.location = [[BMKLocationService alloc] init];
    22.     self.location.delegate = self;
    23.     [self.location startUserLocationService];
    24. }
    25. - (void)viewWillAppear:(BOOL)animated
    26. {
    27.     [super viewWillAppear:animated];
    28.     [self.mapView viewWillAppear];
    29. }
    30. - (void)viewWillDisappear:(BOOL)animated
    31. {
    32.     [super viewWillDisappear:animated];
    33.     [self.mapView viewWillDisappear];
    34. }
    35. - (BMKMapView *)mapView
    36. {
    37.     if (!_mapView) {
    38.         _mapView = [[BMKMapView alloc] init];
    39.         _mapView.delegate = self;
    40.     }
    41.     return _mapView;
    42. }
    43. - (BMKRouteSearch *)routeSearch
    44. {
    45.     if (!_routeSearch) {
    46.         _routeSearch = [[BMKRouteSearch alloc] init];
    47.         _routeSearch.delegate = self;
    48.     }
    49.     return _routeSearch;
    50. }
    51. #pragma mark 定位服务的代理方法,如果获取到位置信息则调用
    52. - (void)didUpdateUserLocation:(BMKUserLocation *)userLocation
    53. {
    54.     // 定位成功后将当前的位置设置为地图的中心点
    55.     self.mapView.centerCoordinate = userLocation.location.coordinate;
    56.     // 地图的缩放比例的17 (3 ~ 19取值)
    57.     self.mapView.zoomLevel = 13;
    58.     // 停止定位服务
    59.     [self.location stopUserLocationService];
    60.     // 显示我当前的位置
    61.     self.mapView.showsUserLocation = YES;
    62.     [self.mapView updateLocationData:userLocation];
    63. }
    64. #pragma mark 点击空白处退出键盘
    65. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    66. {
    67.     [self.view endEditing:YES];
    68. }
    69. #pragma mark 搜索事件
    70. - (IBAction)search
    71. {
    72.     [self.view endEditing:YES];
    73.     // 创建一个起始点
    74.     BMKPlanNode *start = [[BMKPlanNode alloc] init];
    75.     start.name = self.start.text;
    76.     start.cityName = @"北京";
    77.    
    78.     // 创建一个终点
    79.     BMKPlanNode *end = [[BMKPlanNode alloc] init];
    80.     end.name = self.end.text;
    81.     end.cityName = @"北京";
    82.    
    83.     // 创建一个步行选项
    84.     BMKWalkingRoutePlanOption *option = [[BMKWalkingRoutePlanOption alloc] init];
    85.     option.from = start;
    86.     option.to = end;
    87.    
    88.     // 搜索步行选项结果(结果在代理方法中去取)
    89.     [self.routeSearch walkingSearch:option];
    90.    
    91. }
    92. #pragma mark 获取步行路径数据
    93. - (void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error
    94. {
    95.     // 清空地图上所有的标注及所有覆盖物
    96.     NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    97.         [_mapView removeAnnotations:array];
    98.         array = [NSArray arrayWithArray:_mapView.overlays];
    99.         [_mapView removeOverlays:array];
    100.    
    101.     // 返回正确结果
    102.         if (error == BMK_SEARCH_NO_ERROR) {
    103.         // 1、取出路径数据中的第一条(一般可能会有多条路径可选)
    104.         BMKWalkingRouteLine *plan = [result.routes firstObject];
    105.         // 将地图中心点转到路径的起点
    106.         self.mapView.centerCoordinate = plan.starting.location;
    107.         self.mapView.zoomLevel = 14.0f;
    108.         
    109.         // 2、在地图上标注各路段点
    110.         int counts = 0;
    111.         for (int i = 0; i < plan.steps.count; i++) {
    112.             
    113.             // 2.1、起点
    114.             if (i == 0) {
    115.                 // 初始化一个标注点
    116.                 BMKPointAnnotation *point = [[BMKPointAnnotation alloc] init];
    117.                 // 设定标注点坐标
    118.                 point.coordinate = plan.starting.location;
    119.                 // 设定标注点标题
    120.                 point.title = @"起点";
    121.                 // 添加到地图
    122.                 [self.mapView addAnnotation:point];
    123.                
    124.             // 2.2、终点
    125.             }else if (i == plan.steps.count - 1) {
    126.                 BMKPointAnnotation *point = [[BMKPointAnnotation alloc] init];
    127.                 point.coordinate = plan.terminal.location;
    128.                 point.title = @"终点";
    129.                 [self.mapView addAnnotation:point];
    130.                
    131.             // 2.3、中间路段起点
    132.             }else{
    133.                 BMKWalkingStep *step = plan.steps[i];
    134.                 BMKPointAnnotation *point = [[BMKPointAnnotation alloc] init];
    135.                 point.coordinate = step.entrace.location;
    136.                 point.title = step.entraceInstruction;
    137.                 [self.mapView addAnnotation:point];
    138.             }
    139.             // 计算出共经过多少个地理坐标,为后结创建数组提供长度
    140.             counts +=[plan.steps[i] pointsCount];
    141.         }
    142.         
    143.         // 3、取出路段所经过的地理坐标集合(c语言数组)
    144.         BMKMapPoint temppoints[counts];
    145.         
    146.         int count = 0;
    147.         for (BMKWalkingStep *step in plan.steps) {
    148.             for (int i = 0; i < step.pointsCount; i++){
    149.                 temppoints[count] = step.points[i];
    150.                 count++;
    151.             }
    152.         }
    153.         // 4、将坐标点集合进行连线,生成折线添加到地图
    154.                 BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:counts];
    155.                 [_mapView addOverlay:polyLine];
    156.         }
    157. }
    158. #pragma mark 地图添加覆盖物调用的代理方法,根据覆盖物描述生成视图显示到地图上
    159. - (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id<BMKOverlay>)overlay
    160. {
    161.         if ([overlay isKindOfClass:[BMKPolyline class]]) {
    162.         BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
    163.         // 填充颜色
    164.         polylineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
    165.         // 画笔宽度
    166.         polylineView.lineWidth = 3.0;
    167.         return polylineView;
    168.     }
    169.         return nil;
    170. }
    171. @end
    复制代码
    四、Demo下载:
    游客,如果您要查看本帖隐藏内容请回复
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-4-29 03:53 , Processed in 0.059187 second(s), 25 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表