您现在的位置: 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];

        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.coordinate = tempNode.pt;
                item.type = 5;
                item.title = tempNode.name;
                [_mapView addAnnotation:item];
            }
        }

        // 軌跡點
        BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
        // ...(後續依 pointsCount 逐一填入各 step 的點)
    }
}

把路線縮放到可視範圍

關鍵在於先算出所有點的經緯度包圍盒,再透過座標區域與視圖矩形之間的雙向轉換,讓百度地圖把整段路線一次顯示進來。

- (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];
}

這裡有兩個實作上的細節值得注意:一是 convertRegion:toRectToView: 得到的矩形可能超出視圖邊界,實務上常會再對寬高乘上一個留白係數(例如 1.2)避免路線貼邊;二是經度差在跨越 180 度經線時會算出錯誤的跨度,遇到這種資料需要另外處理。歡迎大家批評指正。

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