百度地圖3.1.0路線規(guī)劃

首先按照百度地圖引入SDK 官網上都有教程 這里就不多說了

引入相關的頭文件 看百度demo里面 下面的是百度demo里面的文件 可以去那里拉
(這里自己簡單整理一下 方便自己查看)
<pre>

import <MapKit/MapKit.h>

import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相關所有的頭文件

import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地圖功能所有的頭文件

import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入檢索功能所有的頭文件

import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云檢索功能所有的頭文件

import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的頭文件

import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入計算工具所有的頭文件

import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周邊雷達功能所有的頭文件

//只引入所需的單個頭文件

import "RouteAnnotation.h"

import "UIImage+Rotate.h"

</pre>

聲明變量和協(xié)議

<pre>
@interface StorePathViewController ()<BMKMapViewDelegate,BMKRouteSearchDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>

/** 地圖視圖 /
//@property (nonatomic, strong) BMKMapView
mapView;
@property (weak, nonatomic) IBOutlet BMKMapView mapView;
/
* 搜索 /
@property (nonatomic, strong) BMKRouteSearch
routesearch;//路線查找
@property (nonatomic, strong) BMKLocationService *locService;//定位

@end
</pre>

按照百度地圖文檔進行設置代理個釋放

<pre>

  • (void)viewWillAppear:(BOOL)animated
    {
    // [super viewWillAppear:animated];
    [self.mapView viewWillAppear];
    self.mapView.delegate = self;
    self.routesearch.delegate = self;
    self.locService.delegate =self;
    }
  • (void)viewWillDisappear:(BOOL)animated
    {
    [self.mapView viewWillDisappear];
    self.mapView.delegate = nil;// 此處記得不用的時候需要置nil,否則影響內存的釋放
    self.routesearch.delegate = nil;
    self.locService.delegate = nil;
    }
    </pre>

懶加載

<pre>
//搜索路線

  • (BMKRouteSearch *)routesearch{
    if (_routesearch == nil) {
    _routesearch = [[BMKRouteSearch alloc] init];
    }
    return _routesearch;
    }
    //定位

  • (BMKLocationService*)locService {
    if (_locService == nil) {
    _locService = [[BMKLocationService alloc] init];

    }
    return _locService;

}
</pre>

定位開始

<pre>

  • (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.mapView];
    [_mapView setZoomEnabled:YES];
    [_mapView setZoomLevel:13];//級別,3-19
    _mapView.showMapScaleBar = YES;//比例尺
    _mapView.showsUserLocation=YES;//顯示當前設備的位置
    _mapView.showMapScaleBar = YES;

    _mapView.userTrackingMode = BMKUserTrackingModeFollow;//定位跟隨模式

    [self.locService startUserLocationService];
    [self storeLocationStars];
    }
    </pre>

獲取當前位置

<pre>
-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
[_mapView updateLocationData:userLocation];

CLLocationCoordinate2D coordinate = userLocation.location.coordinate;
startlocation = coordinate;

}
</pre>

對目的地進行標注

<pre>

  • (void)storeLocationStars {
    if (_mainListModel.latitude && _mainListModel.longitude) {
    BMKPointAnnotation *point1 = [[BMKPointAnnotation alloc]init];

      point1.coordinate = CLLocationCoordinate2DMake([_mainListModel.latitude doubleValue], [_mainListModel.longitude doubleValue]);
      endLocation = point1.coordinate;
      point1.title = _mainListModel.name;
      
      [_mapView addAnnotation:point1];
    

    }

}
</pre>

開始位置和目的地位置加入檢索信息

pragma mark - 開始和終點位置

<pre>

  • (BMKPlanNode *)startPt {

    BMKPlanNode* start = [[BMKPlanNode alloc]init];
    start.pt = startlocation;
    return start;

}

  • (BMKPlanNode )endPt {
    BMKPlanNode
    end = [[BMKPlanNode alloc]init];
    end.pt = endLocation;
    return end;
    }
    </pre>

公交路線開始檢索

<pre>
-(IBAction)onClickBusSearch
{

 BMKMassTransitRoutePlanOption *MassTransitRouteSearchOption = [[BMKMassTransitRoutePlanOption alloc]init];
MassTransitRouteSearchOption.from = [self startPt];
MassTransitRouteSearchOption.to = [self endPt];
BOOL flag = [_routesearch massTransitSearch:MassTransitRouteSearchOption];
[SVProgressHUD showWithStatus:SearchPathMsg];

if(flag)
{
    NSLog(@"bus檢索發(fā)送成功");
}
else
{
    NSLog(@"bus檢索發(fā)送失敗");
}

}
</pre>

駕車路線開始檢索

<pre>

pragma mark 駕車搜索

-(IBAction)onClickDriveSearch
{
//[self.mapView removeFromSuperview];

/// 駕車查詢基礎信息類
BMKDrivingRoutePlanOption *drivingRouteSearchOption = [[BMKDrivingRoutePlanOption alloc]init];
drivingRouteSearchOption.from = [self startPt];
drivingRouteSearchOption.to = [self endPt];
BOOL flag = [_routesearch drivingSearch:drivingRouteSearchOption];
if(flag)
{
    NSLog(@"car檢索發(fā)送成功");
}
else
{
    NSLog(@"car檢索發(fā)送失敗");
}

[SVProgressHUD showWithStatus:SearchPathMsg];

}
</pre>

步行路線開始檢索

<pre>

pragma mark 步行搜索

-(IBAction)onClickWalkSearch
{

/// 步行查詢基礎信息類
BMKWalkingRoutePlanOption *walkingRouteSearchOption = [[BMKWalkingRoutePlanOption alloc]init];
walkingRouteSearchOption.from = [self startPt];
walkingRouteSearchOption.to = [self endPt];
BOOL flag = [_routesearch walkingSearch:walkingRouteSearchOption];
[SVProgressHUD showWithStatus:SearchPathMsg];

if(flag)
{
    NSLog(@"walk檢索發(fā)送成功");
}
else
{
    NSLog(@"walk檢索發(fā)送失敗");
}

}</pre>

三種路線的計算 (直接拷貝過來的)

<pre>

pragma mark 公交路線

  • (void)onGetMassTransitRouteResult:(BMKRouteSearch)searcher result:(BMKMassTransitRouteResult)result errorCode:(BMKSearchErrorCode)error {
    NSArray* array = [NSArray arrayWithArray:self.mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    [self storeLocationStars];

    if (error == BMK_SEARCH_NO_ERROR) {
    [SVProgressHUD dismiss];
    BMKMassTransitRouteLine* routeLine = (BMKMassTransitRouteLine*)[result.routes objectAtIndex:0];

      BOOL startCoorIsNull = YES;
      CLLocationCoordinate2D startCoor;//起點經緯度
      CLLocationCoordinate2D endCoor;//終點經緯度
      
      NSInteger size = [routeLine.steps count];
      NSInteger planPointCounts = 0;
      for (NSInteger i = 0; i < size; i++) {
          BMKMassTransitStep* transitStep = [routeLine.steps objectAtIndex:i];
          for (BMKMassTransitSubStep *subStep in transitStep.steps) {
              //添加annotation節(jié)點
              RouteAnnotation* item = [[RouteAnnotation alloc]init];
              item.coordinate = subStep.entraceCoor;
              item.title = subStep.instructions;
              item.type = 2;
              [_mapView addAnnotation:item];
              
              if (startCoorIsNull) {
                  startCoor = subStep.entraceCoor;
                  startCoorIsNull = NO;
              }
              endCoor = subStep.exitCoor;
              
              //軌跡點總數累計
              planPointCounts += subStep.pointsCount;
              
              //steps中是方案還是子路段,YES:steps是BMKMassTransitStep的子路段(A到B需要經過多個steps);NO:steps是多個方案(A到B有多個方案選擇)
              if (transitStep.isSubStep == NO) {//是子方案,只取第一條方案
                  break;
              }
              else {
                  //是子路段,需要完整遍歷transitStep.steps
              }
          }
      }
      
      //添加起點標注
      RouteAnnotation* startAnnotation = [[RouteAnnotation alloc]init];
      startAnnotation.coordinate = startCoor;
      startAnnotation.title = @"起點";
      startAnnotation.type = 0;
      [_mapView addAnnotation:startAnnotation]; // 添加起點標注
      //添加終點標注
      RouteAnnotation* endAnnotation = [[RouteAnnotation alloc]init];
      endAnnotation.coordinate = endCoor;
      endAnnotation.title = @"終點";
      endAnnotation.type = 1;
      [_mapView addAnnotation:endAnnotation]; // 添加起點標注
      
      //軌跡點
      BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
      NSInteger index = 0;
      for (BMKMassTransitStep* transitStep in routeLine.steps) {
          for (BMKMassTransitSubStep *subStep in transitStep.steps) {
              for (NSInteger i = 0; i < subStep.pointsCount; i++) {
                  temppoints[index].x = subStep.points[i].x;
                  temppoints[index].y = subStep.points[i].y;
                  index++;
              }
              
              //steps中是方案還是子路段,YES:steps是BMKMassTransitStep的子路段(A到B需要經過多個steps);NO:steps是多個方案(A到B有多個方案選擇)
              if (transitStep.isSubStep == NO) {//是子方案,只取第一條方案
                  break;
              }
              else {
                  //是子路段,需要完整遍歷transitStep.steps
              }
          }
      }
      
      // 通過points構建BMKPolyline
      BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
      [_mapView addOverlay:polyLine]; // 添加路線overlay
      delete []temppoints;
      [self mapViewFitPolyLine:polyLine];
    

    }
    else {
    [SVProgressHUD showErrorWithStatus:ErrorPathMsg];
    }

}
</pre>

駕車

<pre>
//駕車的路線繪制

  • (void)onGetDrivingRouteResult:(BMKRouteSearch)searcher result:(BMKDrivingRouteResult)result errorCode:(BMKSearchErrorCode)error
    {
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    [self storeLocationStars];
    if (error == BMK_SEARCH_NO_ERROR) {
    [SVProgressHUD dismiss];
    BMKDrivingRouteLine* plan = (BMKDrivingRouteLine)[result.routes objectAtIndex:0];
    // 計算路線方案中的路段數目
    NSInteger size = [plan.steps count];
    int planPointCounts = 0;
    for (int i = 0; i < size; i++) {
    BMKDrivingStep
    transitStep = [plan.steps objectAtIndex:i];
    if(i==0){
    RouteAnnotation* item = [[RouteAnnotation alloc]init];
    item.coordinate = plan.starting.location;
    item.title = @"起點";
    item.type = 0;
    [_mapView addAnnotation:item]; // 添加起點標注

          }else if(i==size-1){
              RouteAnnotation* item = [[RouteAnnotation alloc]init];
              item.coordinate = plan.terminal.location;
              item.title = @"終點";
              item.type = 1;
              [_mapView addAnnotation:item]; // 添加起點標注
          }
          //添加annotation節(jié)點
          RouteAnnotation* item = [[RouteAnnotation alloc]init];
          item.coordinate = transitStep.entrace.location;
          item.title = transitStep.entraceInstruction;
          item.degree = transitStep.direction * 30;
          item.type = 4;
          [_mapView addAnnotation:item];
          
          
          //軌跡點總數累計
          planPointCounts += transitStep.pointsCount;
      }
      // 添加途經點
      if (plan.wayPoints) {
          for (BMKPlanNode* tempNode in plan.wayPoints) {
              RouteAnnotation* item = [[RouteAnnotation alloc]init];
              item = [[RouteAnnotation alloc]init];
              item.coordinate = tempNode.pt;
              item.type = 5;
              item.title = tempNode.name;
              [_mapView addAnnotation:item];
          }
      }
      //軌跡點
      
      BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
      int i = 0;
      for (int j = 0; j < size; j++) {
          BMKDrivingStep* transitStep = [plan.steps objectAtIndex:j];
          int k=0;
          for(k=0;k<transitStep.pointsCount;k++) {
              temppoints[i].x = transitStep.points[k].x;
              temppoints[i].y = transitStep.points[k].y;
              i++;
          }
          
      }
      // 通過points構建BMKPolyline
      BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
      [_mapView addOverlay:polyLine]; // 添加路線overlay
      delete []temppoints;
      [self mapViewFitPolyLine:polyLine];
    

    }else {
    [SVProgressHUD showErrorWithStatus:ErrorPathMsg];
    }

}
</pre>

步行

<pre>
//步行的路線繪制

  • (void)onGetWalkingRouteResult:(BMKRouteSearch)searcher result:(BMKWalkingRouteResult)result errorCode:(BMKSearchErrorCode)error
    {

    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    [self storeLocationStars];
    if (error == BMK_SEARCH_NO_ERROR) {
    [SVProgressHUD dismiss];
    BMKWalkingRouteLine* plan = (BMKWalkingRouteLine)[result.routes objectAtIndex:0];
    NSInteger size = [plan.steps count];
    int planPointCounts = 0;
    for (int i = 0; i < size; i++) {
    BMKWalkingStep
    transitStep = [plan.steps objectAtIndex:i];
    if(i==0){
    RouteAnnotation* item = [[RouteAnnotation alloc]init];
    item.coordinate = plan.starting.location;
    item.title = @"起點";
    item.type = 0;
    [_mapView addAnnotation:item]; // 添加起點標注

          }else if(i==size-1){
              RouteAnnotation* item = [[RouteAnnotation alloc]init];
              item.coordinate = plan.terminal.location;
              item.title = @"終點";
              item.type = 1;
              [_mapView addAnnotation:item]; // 添加起點標注
                      }
          //添加annotation節(jié)點
          RouteAnnotation* item = [[RouteAnnotation alloc]init];
          item.coordinate = transitStep.entrace.location;
          item.title = transitStep.entraceInstruction;
          item.degree = transitStep.direction * 30;
          item.type = 4;
          [_mapView addAnnotation:item];
          
          //軌跡點總數累計
          planPointCounts += transitStep.pointsCount;
      }
      
      //軌跡點
      BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
      int i = 0;
      for (int j = 0; j < size; j++) {
          BMKWalkingStep* transitStep = [plan.steps objectAtIndex:j];
          int k=0;
          for(k=0;k<transitStep.pointsCount;k++) {
              temppoints[i].x = transitStep.points[k].x;
              temppoints[i].y = transitStep.points[k].y;
              i++;
          }
          
      }
      // 通過points構建BMKPolyline
      BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
      [_mapView addOverlay:polyLine]; // 添加路線overlay
      delete []temppoints;
    

    }else {
    [SVProgressHUD showErrorWithStatus:ErrorPathMsg];
    }

}
</pre>

<pre>
//路線標記

  • (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
    {

    if ([annotation isKindOfClass:[RouteAnnotation class]]) {
    // return [self getRouteAnnotationView:view viewForAnnotation:(RouteAnnotation)annotation];
    return [(RouteAnnotation
    )annotation getRouteAnnotationView:view];
    }

    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
    BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
    newAnnotationView.pinColor = BMKPinAnnotationColorRed;
    newAnnotationView.animatesDrop = YES;// 設置該標注點動畫顯示
    return newAnnotationView;
    }
    return nil;
    }
    //加載線路

  • (BMKOverlayView*)mapView:(BMKMapView )map viewForOverlay:(id<BMKOverlay>)overlay
    {
    if ([overlay isKindOfClass:[BMKPolyline class]]) {
    BMKPolylineView
    polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
    polylineView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:1];
    polylineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
    polylineView.lineWidth = 3.0;
    return polylineView;
    }
    return nil;
    }
    </pre>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,563評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 99,694評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,672評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,965評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,690評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,019評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,013評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,188評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 49,718評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,438評論 3 360
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,667評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,149評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,845評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,252評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,590評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,384評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,635評論 2 380

推薦閱讀更多精彩內容