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

来源:互联网 发布:淘宝买家延长收货 编辑:程序博客网 时间:2024/06/10 14:47

上篇介绍到了附近的POI搜索,在这篇我要给大家介绍一下路径的POI搜索。路径搜索分三种:步行、公交、自驾。写代码之前需要遵循BMKRouteSearchDelegate代理,注意代码中的起始点位置由用户自己手动输入,我的demo页面设计如下图


1.步行路线的搜索

- (IBAction)footBtnTouched:(id)sender {

   _aView.hidden =YES;

    search = [[BMKRouteSearchalloc]init];

    search.delegate =self;

    //发起检索

    BMKPlanNode *start = [[BMKPlanNodealloc]init];

    start.name =_serchTF.text;//搜索路径起点

    start.cityName =@"北京市";

    BMKPlanNode *end = [[BMKPlanNodealloc]init];

    end.name =_search2TF.text;//搜索路径终点

    end.cityName =@"北京市";

    BMKWalkingRoutePlanOption *walkRouteSearchOption =         [[BMKWalkingRoutePlanOptionalloc]init];

    walkRouteSearchOption.from = start;

    walkRouteSearchOption.to = end;

   BOOL flag1 = [searchwalkingSearch:walkRouteSearchOption];

   if(flag1)

    {

        NSLog(@"foot检索发送成功");

    }

   else

    {

        NSLog(@"foot检索发送失败");

    }

}

搜索成功后要实现他的代理方法

#pragma mark -- 路线poi检索 --

-(void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error

{

   NSArray* array = [NSArrayarrayWithArray:MapView.annotations];

    [MapViewremoveAnnotations:array];

    array = [NSArrayarrayWithArray:MapView.overlays];

    [MapViewremoveOverlays:array];

    if (error ==BMK_SEARCH_NO_ERROR) {

        BMKWalkingRouteLine *plan = (BMKWalkingRouteLine*)[result.routesobjectAtIndex:0];//表示一条步行路线

       int size = [plan.stepscount];

       int planPointCounts = 0;

       for (int i =0; i < size; i++) {

           BMKWalkingStep *transitStep = [plan.stepsobjectAtIndex:i];//表示步行中的一个路段

           if(i==0){

                BMKPointAnnotation *item = [[BMKPointAnnotationalloc] init];

                item.coordinate = plan.starting.location;

                item.title =@"起点";

                [MapViewaddAnnotation:item]; // 添加起点标注

            }elseif(i==size-1){

                BMKPointAnnotation *item = [[BMKPointAnnotationalloc] init];

                item.coordinate = plan.terminal.location;

                item.title =@"终点";

                [MapViewaddAnnotation:item]; // 添加起点标注

            }

           //轨迹点

            planPointCounts += transitStep.pointsCount;

        }

       BMKMapPoint *temppoints = new BMKMapPoint[planPointCounts];//地理坐标点

       int i = 0;

       for (int j =0; j < size; j++) {

           BMKWalkingStep *transitStep = [plan.stepsobjectAtIndex: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 = [BMKPolylinepolylineWithPoints:temppoints count:planPointCounts];

        [MapViewaddOverlay:polyLine]; // 添加路线overlay

    }

    elseif (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 = [[BMKPolylineViewalloc] initWithOverlay:overlay];

        polylineView.strokeColor = [[UIColorredColor] colorWithAlphaComponent:1];

        polylineView.lineWidth =5.0;

        return polylineView;

    }

    return nil;

}

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



2.公交路线搜索

- (IBAction)busBtnTouched:(id)sender {

    search = [[BMKRouteSearchalloc]init];

    search.delegate =self;

    //发起检索

    BMKPlanNode *start = [[BMKPlanNodealloc]init];

    start.name =_serchTF.text;

    BMKPlanNode *end = [[BMKPlanNodealloc]init];

    end.name =_search2TF.text;

    BMKTransitRoutePlanOption *transitRouteSearchOption =         [[BMKTransitRoutePlanOptionalloc]init];

    transitRouteSearchOption.city =@"北京市";//在那个城市搜索

    transitRouteSearchOption.from = start;

    transitRouteSearchOption.to = end;

   BOOL flag1 = [searchtransitSearch:transitRouteSearchOption];

   if(flag1)

    {

        NSLog(@"bus检索发送成功");

    }

   else

    {

        NSLog(@"bus检索发送失败");

    }

}

#pragma mark -- 公交路线代理实现 --

- (void)onGetTransitRouteResult:(BMKRouteSearch *)searcher result:(BMKTransitRouteResult *)result errorCode:(BMKSearchErrorCode)error

{

    [routArrremoveAllObjects];

    [allRoutArrremoveAllObjects];

   NSArray* array = [NSArrayarrayWithArray:MapView.annotations];

    [MapViewremoveAnnotations:array];

    array = [NSArrayarrayWithArray:MapView.overlays];

    [MapViewremoveOverlays:array];

    if (error ==BMK_SEARCH_NO_ERROR) {

       for (int n =0; n< result.routes.count; n++) {

            BMKTransitRouteLine *plan = (BMKTransitRouteLine*)[result.routesobjectAtIndex:n];

           int size = [plan.stepscount];

           for (int i =0; i < size; i++) {

               BMKTransitStep *transitStep = [plan.stepsobjectAtIndex:i];//公交路线的换乘路段

             transitStep.instruction//换乘路段说明,下面打印出来的结果就是每段的换乘说明,这个结果你想怎么出炉就怎么处理

          }

       }

    }

}

下边是每个路段的换乘说明,大家可以按照自己的想法处理这个结果,至于每个路段经过的坐标点,大家可以参照步行的那个demo,基本都是一样的


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


0 0
原创粉丝点击