你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS 地圖線路動態可視化顯示

iOS 地圖線路動態可視化顯示

編輯:IOS開發綜合
之前有碰到過這樣的問題,就是畫出兩點之間的距離,然後將線路顯示在可視化的范圍內,下面是一些主要代碼:
#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

- (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];
}
歡迎大家批評指正!!!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved