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

來源:互聯網
上載者:User

標籤:地圖   可視化   線路圖   

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.