上篇介紹到了附近的POI搜索,在這篇我要給大家介紹一下路徑的POI搜索。路徑搜索分三種:步行、公交、自駕。寫代碼之前需要遵循BMKRouteSearchDelegate代理,注意代碼中的起始點位置由用戶自己手動輸入,我的demo頁面設計如下圖
1.步行路線的搜索
- (IBAction)footBtnTouched:(id)sender {
_aView.hidden = YES;
search = [[BMKRouteSearch alloc]init];
search.delegate = self;
//發起檢索
BMKPlanNode *start = [[BMKPlanNode alloc]init];
start.name = _serchTF.text;//搜索路徑起點
start.cityName = @"北京市";
BMKPlanNode *end = [[BMKPlanNode alloc]init];
end.name = _search2TF.text;//搜索路徑終點
end.cityName = @"北京市";
BMKWalkingRoutePlanOption *walkRouteSearchOption = [[BMKWalkingRoutePlanOption alloc]init];
walkRouteSearchOption.from = start;
walkRouteSearchOption.to = end;
BOOL flag1 = [search walkingSearch:walkRouteSearchOption];
if(flag1)
{
NSLog(@"foot檢索發送成功");
}
else
{
NSLog(@"foot檢索發送失敗");
}
}
#pragma mark -- 路線poi檢索 --
-(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];
if (error == BMK_SEARCH_NO_ERROR) {
BMKWalkingRouteLine *plan = (BMKWalkingRouteLine*)[result.routes objectAtIndex:0];//表示一條步行路線
int size = [plan.steps count];
int planPointCounts = 0;
for (int i = 0; i < size; i++) {
BMKWalkingStep *transitStep = [plan.steps objectAtIndex:i];//表示步行中的一個路段
if(i==0){
BMKPointAnnotation *item = [[BMKPointAnnotation alloc] init];
item.coordinate = plan.starting.location;
item.title = @"起點";
[MapView addAnnotation:item]; // 添加起點標註
}else if(i==size-1){
BMKPointAnnotation *item = [[BMKPointAnnotation alloc] init];
item.coordinate = plan.terminal.location;
item.title = @"終點";
[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];
for(int k=0;k<transitStep.pointsCount;k++) {
temppoints[i].x = transitStep.points[k].x;
temppoints[i].y = transitStep.points[k].y;
i++;
}
}
[MapView setCenterCoordinate:plan.starting.location];
// 通過points構建BMKPolyline
BMKPolyline *polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
[MapView addOverlay:polyLine]; // 添加路線overlay
}
else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
//當路線起終點有歧義時通,獲取建議檢索起終點
}
else {
NSLog(@"抱歉,未找到結果");
}
search.delegate = nil;
}
#pragma mark -- 繪製路線折線圖,此處實現的是BMKMapViewDelegate--
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKPolyline class]]){
BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
polylineView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:1];
polylineView.lineWidth = 5.0;
return polylineView;
}
return nil;
}
此時的運行結果如下圖所示- (IBAction)busBtnTouched:(id)sender {
search = [[BMKRouteSearch alloc]init];
search.delegate = self;
//發起檢索
BMKPlanNode *start = [[BMKPlanNode alloc]init];
start.name = _serchTF.text;
BMKPlanNode *end = [[BMKPlanNode alloc]init];
end.name = _search2TF.text;
BMKTransitRoutePlanOption *transitRouteSearchOption = [[BMKTransitRoutePlanOption alloc]init];
transitRouteSearchOption.city = @"北京市";//在那個城市搜索
transitRouteSearchOption.from = start;
transitRouteSearchOption.to = end;
BOOL flag1 = [search transitSearch:transitRouteSearchOption];
if(flag1)
{
NSLog(@"bus檢索發送成功");
}
else
{
NSLog(@"bus檢索發送失敗");
}
}
#pragma mark -- 公交路線代理實現 --
- (void)onGetTransitRouteResult:(BMKRouteSearch *)searcher result:(BMKTransitRouteResult *)result errorCode:(BMKSearchErrorCode)error
{
[routArr removeAllObjects];
[allRoutArr removeAllObjects];
NSArray* array = [NSArray arrayWithArray:MapView.annotations];
[MapView removeAnnotations:array];
array = [NSArray arrayWithArray:MapView.overlays];
[MapView removeOverlays:array];
if (error == BMK_SEARCH_NO_ERROR) {
for (int n = 0; n< result.routes.count; n++) {
BMKTransitRouteLine *plan = (BMKTransitRouteLine*)[result.routes objectAtIndex:n];
int size = [plan.steps count];
for (int i = 0; i < size; i++) {
BMKTransitStep *transitStep = [plan.steps objectAtIndex:i];//公交路線的換乘路段
transitStep.instruction//換乘路段說明,下面打印出來的結果就是每段的換乘說明,這個結果你想怎麼出爐就怎麼處理
}
}
}
}
下邊是每個路段的換乘說明,大家可以按照自己的想法處理這個結果,至於每個路段經過的座標點,大家可以參照步行的那個demo,基本都是一樣的