iOS基于百度地图的开发(4)——路径搜索

本文通过起始点左边和终点坐标来说明接口的使用方法。

1. 进行路径搜索

BMKSearch* search = [[BMKSearch alloc] init]; //创建搜索对象,注意不要释放该对象
search.delegate = self; //设置路径搜索结果回调代理为自身
BMKPlanNode* currentNode = [[BMKPlanNode alloc] init]; 
currentNode.pt = theMapView.userLocation.coordinate;
currentNode.name = @"aaa"; //注意,这里不能为空@""
BMKPlanNode* poiNode = [[BMKPlanNode alloc] init];
poiNode.pt = vehicleLocation.coordinate;
poiNode.name = @"bbb";//注意,这里不能为空@""
[search drivingRouteSearch:nil startNode:currentNode endCity:nil endNode:poiNode];//仅以搜索驾驶路线为例

2. 通过路线搜索结果回调来处理路线

- (void)onGetDrivingRouteResult:(BMKPlanResult*)result errorCode:(int)error
{
    if (error == BMKErrorOk) 
    {
	BMKRoutePlan* plan = (BMKRoutePlan*)[result.plans objectAtIndex:0];

        BMKRoute* route = [plan.routes objectAtIndex:0];

        BMKMapPoint* points = malloc(sizeof(CLLocationCoordinate2D) * route.steps.count);
        
        for (int i = 0; i < route.steps.count; ++i) 
        {
            BMKStep* step = [route.steps objectAtIndex:i];
            BMKMapPoint point = BMKMapPointForCoordinate(step.pt); //注意这里的处理方法
            points[i] = point;
        }
        
        [mapView removeOverlays:[mapView overlays]];
        BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:points count:route.steps.count];
        free(points);
        [mapView addOverlay:polyLine];
    }
}

3. 在处理路径回调中处理路径的显示

- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
{
    BMKOverlayView *result = [[[BMKPolylineView alloc] initWithPolyline:overlay] autorelease];
    result.strokeColor = [UIColor blueColor];
    result.lineWidth = 4.0;
    result.lineJoin = kCGLineJoinRound;
    result.lineCap = kCGLineCapRound;

    return result;
}

iOS基于百度地图的开发(1)——用户当前位置

iOS基于百度地图的开发(2)——获取POI

iOS基于百度地图的开发(3)——地址搜索

iOS基于百度地图的开发(4)——路径搜索

iOS基于百度地图的开发(5)——核心问题汇总

iOS基于百度地图的开发(6)——google坐标转换成百度坐标

iOS基于百度地图的开发(7)——百度坐标转换成Google坐标


你可能感兴趣的:(iOS基于百度地图的开发(4)——路径搜索)