iOS 地图线路动态可视化显示

本文详细阐述了如何使用代码实现驾车线路检索,并将其可视化显示在地图上,包括处理正常结果、添加起点和终点标注、计算并绘制路线、以及设置地图可视范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前有碰到过这样的问题,就是画出两点之间的距离,然后将线路显示在可视化的范围内,下面是一些主要代码:
#pragma mark - 驾车线路检索
- (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
        [_mapView removeAnnotations:array];
        array = [NSArray arrayWithArray:_mapView.overlays];
        [_mapView removeOverlays:array];
        if (error == BMK_SEARCH_NO_ERROR) {
            BMKDrivingRouteLine* plan = (BMKDrivingRouteLine*)[result.routes objectAtIndex:0];
            // 计算路线方案中的路段数目
            int size = [plan.steps count];
            int planPointCounts = 0;
            for (int i = 0; i < size; i++) {
                BMKDrivingStep* transitStep = [plan.steps objectAtIndex:i];
                if(i==0){
                    RouteAnnotation* item = [[RouteAnnotation alloc]init];
                    item.coordinate = plan.starting.location;
                    item.title = @"起点";
                    item.type = 0;
                    [_mapView addAnnotation:item]; // 添加起点标注
                    
                }else if(i==size-1){
                    RouteAnnotation* item = [[RouteAnnotation alloc]init];
                    item.coordinate = plan.terminal.location;
                    item.title = @"终点";
                    item.type = 1;
                    [_mapView addAnnotation:item]; // 添加起点标注
                }
                //添加annotation节点
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = transitStep.entrace.location;
                item.title = transitStep.entraceInstruction;
                item.degree = transitStep.direction * 30;
                item.type = 4;
                [_mapView addAnnotation:item];
                //轨迹点总数累计
                planPointCounts += transitStep.pointsCount;
                
                //-----------------------------------------------------------
                
                if (i==0) {
                 //以第一个坐标点做初始值
                    minLat = plan.starting.location.latitude;
                    maxLat = plan.starting.location.latitude;
                    minLon = plan.starting.location.longitude;
                    maxLon = plan.starting.location.longitude;
                }else{
                  //对比筛选出最小纬度,最大纬度;最小经度,最大经度
                    minLat = MIN(minLat, transitStep.entrace.location.latitude);
                    maxLat = MAX(maxLat, transitStep.entrace.location.latitude);
                    minLon = MIN(minLon, transitStep.entrace.location.longitude);
                    maxLon = MAX(maxLon, transitStep.entrace.location.longitude);
                }
                
                //-----------------------------------------------------------
            }
            
            [self setVisibleRegin];
            
            // 添加途经点
            if (plan.wayPoints) {
                for (BMKPlanNode* tempNode in plan.wayPoints) {
                    RouteAnnotation* item = [[RouteAnnotation alloc]init];
                    item = [[RouteAnnotation alloc]init];
                    item.coordinate = tempNode.pt;
                    item.type = 5;
                    item.title = tempNode.name;
                    [_mapView addAnnotation:item];
                }
            }
            //轨迹点
            BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
            int i = 0;
            for (int j = 0; j < size; j++) {
                BMKDrivingStep* transitStep = [plan.steps objectAtIndex:j];
                int k=0;
                for(k=0;k<transitStep.pointsCount;k++) {
                    temppoints[i].x = transitStep.points[k].x;
                    temppoints[i].y = transitStep.points[k].y;
                    i++;
                }
                
            }
            // 通过points构建BMKPolyline
            
            BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
            [_mapView addOverlay:polyLine]; // 添加路线overlay
            delete []temppoints;
            
            
        }
    }
    else {
        NSLog(@"抱歉,未找到结果");
    }
}


- (void)setVisibleRegin
{
    //计算中心点
    CLLocationCoordinate2D centCoor;
    centCoor.latitude = (CLLocationDegrees)((maxLat+minLat) * 0.5f);
    centCoor.longitude = (CLLocationDegrees)((maxLon+minLon) * 0.5f);
    BMKCoordinateSpan span;
    //计算地理位置的跨度
    span.latitudeDelta = maxLat - minLat;
    span.longitudeDelta = maxLon - minLon;
    //得出数据的坐标区域
    BMKCoordinateRegion region = BMKCoordinateRegionMake(centCoor, span);
    
    //百度地图的坐标范围转换成相对视图的位置
    CGRect fitRect = [_mapView convertRegion:region toRectToView:_mapView];
    //将地图视图的位置转换成地图的位置,
    BMKMapRect fitMapRect = [_mapView convertRect:fitRect toMapRectFromView:_mapView];
    //设置地图可视范围为数据所在的地图位置
    [_mapView setVisibleMapRect:fitMapRect animated:YES];
}
欢迎大家批评指正!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值