Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Baidu map to realize the smooth movement function after planning the route of the car?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use Baidu map to achieve smooth movement after the car planning route, the article is very detailed, has a certain reference value, interested friends must read it!

Purpose of the article

Project development needs, so combined with Baidu map provides the car smooth track movement, write their own demo

Realize the effect

Here are the key steps for implementation

Integrate Baidu Maps

How to integrate naturally is to look at the documents provided by Baidu Map Development platform.

Document connection

Planned circuit

Look at the document of Baidu map and write a tool for planning the route (driving)

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 ("Test", 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));}}

After planning the route, the real-time road condition index needs to be saved, and it is necessary to draw the map later.

/ / set the real-time road condition index List 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 divideRouteLine(ArrayList routeLine, double distance) { // 截取后的路线点的结果集 ArrayList 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 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;} finalLatLng 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); / / end point}} return result;}

Finally, the child thread is started to update the status of the car (front direction and car position).

/ * cycle mobile logic * / 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)) = 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 ();} / / this is the current point and the next point of the car, which is used to determine the front direction of the car final LatLng startPoint = latLngs.get (mIndex); final LatLng endPoint = latLngs.get (mIndex + 1) MHandler.post (new Runnable () {@ Override public void run () {/ / update the position of the car and the angle of the front if (mMapView = = null) {return;} mMoveMarker.setPosition (startPoint) MMoveMarker.setRotate ((float) getAngle (startPoint, endPoint);}}); try {/ / controls the thread update interval thisThread.sleep (TIME_INTERVAL);} catch (InterruptedException e) {e.printStackTrace () }}; / / start thread moveThread.start ();} above are all the contents of this article entitled "how to use Baidu Map to achieve smooth movement after car route planning". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report