您好,登錄后才能下訂單哦!
小編給大家分享一下百度地圖如何實現小車規劃路線后平滑移動功能,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
實現效果
代碼下載
下載鏈接
下面是實現的關鍵步驟
集成百度地圖
怎么集成自然是看百度地圖開發平臺提供的文檔。
文檔連接
規劃線路
看百度地圖的文檔,寫一個規劃線路的工具類(駕車的)
package com.wzhx.car_smooth_move_demo.utils; import android.util.Log; import com.baidu.mapapi.search.route.BikingRouteResult; import com.baidu.mapapi.search.route.DrivingRoutePlanOption; import com.baidu.mapapi.search.route.DrivingRouteResult; import com.baidu.mapapi.search.route.IndoorRouteResult; import com.baidu.mapapi.search.route.MassTransitRouteResult; import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener; import com.baidu.mapapi.search.route.PlanNode; import com.baidu.mapapi.search.route.RoutePlanSearch; import com.baidu.mapapi.search.route.TransitRouteResult; import com.baidu.mapapi.search.route.WalkingRouteResult; import com.wzhx.car_smooth_move_demo.listener.OnGetDrivingResultListener; public class RoutePlanUtil { private RoutePlanSearch mRoutePlanSearch = RoutePlanSearch.newInstance(); private OnGetDrivingResultListener getDrivingResultListener; private OnGetRoutePlanResultListener getRoutePlanResultListener = new OnGetRoutePlanResultListener() { @Override public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) { } @Override public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) { } @Override public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) { } @Override public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) { Log.e("測試", drivingRouteResult.error + ":" + drivingRouteResult.status); getDrivingResultListener.onSuccess(drivingRouteResult); } @Override public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) { } @Override public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) { } }; public RoutePlanUtil(OnGetDrivingResultListener getDrivingResultListener) { this.getDrivingResultListener = getDrivingResultListener; this.mRoutePlanSearch.setOnGetRoutePlanResultListener(this.getRoutePlanResultListener); } public void routePlan(PlanNode startNode, PlanNode endNode){ mRoutePlanSearch.drivingSearch((new DrivingRoutePlanOption()) .from(startNode).to(endNode) .policy(DrivingRoutePlanOption.DrivingPolicy.ECAR_TIME_FIRST) .trafficPolicy(DrivingRoutePlanOption.DrivingTrafficPolicy.ROUTE_PATH_AND_TRAFFIC)); } }
規劃線路后需要將實時路況索引保存,為后面畫圖需要
// 設置路段實時路況索引 List<DrivingRouteLine.DrivingStep> allStep = selectedRouteLine.getAllStep(); mTrafficTextureIndexList.clear(); for (int j = 0; j < allStep.size(); j++) { if (allStep.get(j).getTrafficList() != null && allStep.get(j).getTrafficList().length > 0) { for (int k = 0; k < allStep.get(j).getTrafficList().length; k++) { mTrafficTextureIndexList.add(allStep.get(j).getTrafficList()[k]); } } }
要將路線規劃的路線上的路段再細分(切割),這樣小車移動才會平滑
/** * 將規劃好的路線點進行截取 * 參考百度給的小車平滑軌跡移動demo實現。(循環的算法不太懂) * @param routeLine * @param distance * @return */ private ArrayList<LatLng> divideRouteLine(ArrayList<LatLng> routeLine, double distance) { // 截取后的路線點的結果集 ArrayList<LatLng> result = new ArrayList<>(); mNewTrafficTextureIndexList.clear(); for (int i = 0; i < routeLine.size() - 1; i++) { final LatLng startPoint = routeLine.get(i); final LatLng endPoint = routeLine.get(i + 1); double slope = getSlope(startPoint, endPoint); // 是不是正向的標示 boolean isYReverse = (startPoint.latitude > endPoint.latitude); boolean isXReverse = (startPoint.longitude > endPoint.longitude); double intercept = getInterception(slope, startPoint); double xMoveDistance = isXReverse ? getXMoveDistance(slope, distance) : -1 * getXMoveDistance(slope, distance); double yMoveDistance = isYReverse ? getYMoveDistance(slope, distance) : -1 * getYMoveDistance(slope, distance); ArrayList<LatLng> temp1 = new ArrayList<>(); for (double j = startPoint.latitude, k = startPoint.longitude; !((j > endPoint.latitude) ^ isYReverse) && !((k > endPoint.longitude) ^ isXReverse); ) { LatLng latLng = null; if (slope == Double.MAX_VALUE) { latLng = new LatLng(j, k); j = j - yMoveDistance; } else if (slope == 0.0) { latLng = new LatLng(j, k - xMoveDistance); k = k - xMoveDistance; } else { latLng = new LatLng(j, (j - intercept) / slope); j = j - yMoveDistance; } final LatLng finalLatLng = latLng; if (finalLatLng.latitude == 0 && finalLatLng.longitude == 0) { continue; } mNewTrafficTextureIndexList.add(mTrafficTextureIndexList.get(i)); temp1.add(finalLatLng); } result.addAll(temp1); if (i == routeLine.size() - 2) { result.add(endPoint); // 終點 } } return result; }
最后是開啟子線程,對小車狀態進行更新(車頭方向和小車位置)
/** * 循環進行移動邏輯 */ public void moveLooper() { moveThread = new Thread() { public void run() { Thread thisThread = Thread.currentThread(); while (!exit) { for (int i = 0; i < latLngs.size() - 1; ) { if (exit) { break; } for (int p = 0; p < latLngs.size() - 1; p++) { // 這是更新索引的條件,這里總是為true // 實際情況可以是:當前誤差小于5米 DistanceUtil.getDistance(mCurrentLatLng, latLngs.get(p)) <= 5) // mCurrentLatLng 這個小車的當前位置得自行獲取得到 if (true) { // 實際情況的索引更新 mIndex = p; mIndex++; // 模擬就是每次加1 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(mContext, "當前索引:" + mIndex, Toast.LENGTH_SHORT).show(); } }); break; } } // 改變循環條件 i = mIndex + 1; if (mIndex >= latLngs.size() - 1) { exit = true; break; } // 擦除走過的路線 int len = mNewTrafficTextureIndexList.subList(mIndex, mNewTrafficTextureIndexList.size()).size(); Integer[] integers = mNewTrafficTextureIndexList.subList(mIndex, mNewTrafficTextureIndexList.size()).toArray(new Integer[len]); int[] index = new int[integers.length]; for (int x = 0; x < integers.length; x++) { index[x] = integers[x]; } if (index.length > 0) { mPolyline.setIndexs(index); mPolyline.setPoints(latLngs.subList(mIndex, latLngs.size())); } // 這里是小車的當前點和下一個點,用于確定車頭方向 final LatLng startPoint = latLngs.get(mIndex); final LatLng endPoint = latLngs.get(mIndex + 1); mHandler.post(new Runnable() { @Override public void run() { // 更新小車的位置和車頭的角度 if (mMapView == null) { return; } mMoveMarker.setPosition(startPoint); mMoveMarker.setRotate((float) getAngle(startPoint, endPoint)); } }); try { // 控制線程更新時間間隔 thisThread.sleep(TIME_INTERVAL); } catch (InterruptedException e) { e.printStackTrace(); } } } } }; // 啟動線程 moveThread.start(); }
看完了這篇文章,相信你對“百度地圖如何實現小車規劃路線后平滑移動功能”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。