ios开发中百度地图的基本使用二

上篇介绍到了附近的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;

}

此时的运行结果如下图所示



2.公交路线搜索

- (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,基本都是一样的


这三种的写法大同小异,搜索出来的路线结果由你自己来处理。所以自驾的我就不写了,参照上面的代码我想大家应该能写出来,只是代理方法换一下而已。到这里百度地图的一些基本功能就都实现了,如果想了解更多就去看看官方的API吧,可以到后面的地址查看:点击打开链接,进入百度开放平台


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值