推荐看完之后注意一下最后的东西
一、细说百度地图的路径规划
路径规划主要有这么几种
1.公交路径规划
1.1 市内公交规划(暂时不在这里说)
1.2 跨市/省公交规划
// 导入头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Map/BMKPolylineView.h>
#import <BaiduMapAPI_Utils/BMKGeometry.h>
#pragma mark: - 公交路线
- (void)showBusSearch {
//线路检索节点信息
BMKPlanNode *start = [[BMKPlanNode alloc] init];
start.pt = self.userLocation.location.coordinate;
start.cityName = @"南宁";
BMKPlanNode *end = [[BMKPlanNode alloc] init];
// 108.296699,22.842406
CLLocationCoordinate2D endCoordinate = CLLocationCoordinate2DMake(22.842406, 108.296699);
end.pt = endCoordinate;
end.cityName = @"南宁";
BMKMassTransitRoutePlanOption *drivingRouteSearchOption = [[BMKMassTransitRoutePlanOption alloc] init];
drivingRouteSearchOption.from = start;
drivingRouteSearchOption.to = end;
BOOL flag = [_routesearch massTransitSearch:drivingRouteSearchOption];
if (flag) {
NSLog(@"%s - 设置成功!",__func__);
}else {
debugLog(@"设置失败");
}
}
/**
*返回公共交通路线检索结果(new)回调方法
*@param searcher 搜索对象
*@param result 搜索结果,类型为BMKMassTransitRouteResult
*@param error 错误号,@see BMKSearchErrorCode
*/
- (void)onGetMassTransitRouteResult:(BMKRouteSearch*)searcher result:(BMKMassTransitRouteResult*)result errorCode:(BMKSearchErrorCode)error {
debugLog(@"公交方案-%@",result.routes);
if (error == BMK_SEARCH_NO_ERROR) {
for (int i = 0; i < result.routes.count; i++ ) {
BMKMassTransitRouteLine *massTransitRouteLine = result.routes[i];
// 保存换乘说明
NSMutableArray *instructions = [NSMutableArray array];
// 换乘的交通工具
NSMutableArray *stepTypes = [NSMutableArray array];
// 价格信息
debugLog(@"价格-%f",massTransitRouteLine.price);
debugLog(@"时间分钟-%d",massTransitRouteLine.duration.minutes);
debugLog(@"起点-%@",massTransitRouteLine.starting.title);
debugLog(@"终点-%@",massTransitRouteLine.terminal.title);
debugLog(@"路段方案%@",massTransitRouteLine.steps);
// 所有路段的信息
for ( int j = 0; j < massTransitRouteLine.steps.count; j++) {
BMKMassTransitStep *step = massTransitRouteLine.steps[j];
debugLog(@"%@",step.steps);
for ( int k = 0; k< step.steps.count; k++) {
BMKMassTransitSubStep *subStep = step.steps[k];
debugLog(@"换乘说明-%@",subStep.instructions);
[instructions addObject:subStep.instructions];
debugLog(@"路段类型-%u",subStep.stepType);
if(subStep.stepType != 5) { // 5为步行
if (subStep.vehicleInfo.name) {
[stepTypes addObject:subStep.vehicleInfo.name];
}
}
// 当路段为公交路段或地铁路段时,可以获取交通工具信息
debugLog(@"交通工具信息-%@",subStep.vehicleInfo.name);
}
}
}
}
}
2.驾车路径规划
#pragma mark 驾车路线
-(void)showDriveSearch {
//线路检索节点信息
BMKPlanNode *start = [[BMKPlanNode alloc] init];
start.pt = self.userLocation.location.coordinate;
start.cityName = @“南宁”;
BMKPlanNode *end = [[BMKPlanNode alloc] init];
CLLocationCoordinate2D endCoordinate = CLLocationCoordinate2DMake(22.842406, 108.296699);
end.pt = endCoordinate;
end.cityName = @"南宁";
BMKDrivingRoutePlanOption *drivingRouteSearchOption = [[BMKDrivingRoutePlanOption alloc] init];
drivingRouteSearchOption.from = start;
drivingRouteSearchOption.to = end;
BOOL flag = [_routesearch drivingSearch:drivingRouteSearchOption];
if (flag) {
NSLog(@"%s - 设置成功!",__func__);
}else {
debugLog(@"设置失败");
}
}
#pragma mark 返回驾乘搜索结果
if (error == BMK_SEARCH_NO_ERROR) {
for (int i = 0; i < result.routes.count; i++) {
NSMutableArray *instruction = [NSMutableArray array];
NSMutableArray *waypoints = [NSMutableArray array];
//表示一条驾车路线
BMKDrivingRouteLine *plan = result.routes[i];
for (int k = 0; k < plan.wayPoints.count; k++) {
BMKPlanNode *node = plan.wayPoints[k];
[waypoints addObject:node.name];
}
for (int j = 0; j < plan.steps.count; j++) {