码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-11-04 17:24:23      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:地图   可视化   线路图   

之前有碰到过这样的问题,就是画出两点之间的距离,然后将线路显示在可视化的范围内,下面是一些主要代码:
#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];
}
欢迎大家批评指正!!!

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

标签:地图   可视化   线路图   

原文地址:http://blog.csdn.net/mdk132/article/details/40787129

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!