Android中高德地圖通過經緯度簡單的實現小車軌跡回放功能
阿新 • • 發佈:2018-11-23
引文:樓主小白,記錄下最近實現的一點簡單小功能,對接高德API簡單實現了小車軌跡回放功能.
效果圖:
實現的功能:簡單實現車輛根據經緯度平滑移動的效果,滑過的線條變紅色,每個經緯度設定了自定義的Marker,後期可以
根據傳入的引數設定Button實現時間,速度,回放等功能.
準備工作:
APP已成功顯示高德地圖,(KEY值,許可權,服務等)並拿到AMap物件
功能流程:
當前在P層寫的功能,傳入的TextView顯示要實現回放的功能和當前Activity上下文,呼叫方法在Activty回撥獲取到介面資料後直接呼叫addMarker()即可。
(1)獲取伺服器返回的經緯度集合,並設定每個經緯度對應的Marker的樣式,並初始化用到的集合和平滑移動物件.
/** * 根據伺服器返回的經緯度建立Marker,實現軌跡回放功能 */ //設定一個存放Marker集合資料 private SmoothMoveMarker smoothMarker; private List<LatLng> points; private List<Marker> listMarker; private List<Polyline> polylineList; public void addMarker(ArrayList<TrackReplayData> tracklist,LocationActivity activity,TextView tv_refresh){ if(tracklist==null){ return; } points = new ArrayList<>(); listMarker = new ArrayList<>(); polylineList = new ArrayList<>(); smoothMarker = new SmoothMoveMarker(aMap); points.clear(); for (int i= 0;i < tracklist.size();i++) { MarkerOptions markerOption = new MarkerOptions(); TrackReplayData trackReplayData = tracklist.get(i); LatLng latLng = new LatLng(trackReplayData.getLatitude(), trackReplayData.getLongitude()); points.add(i,latLng); markerOption.visible(true); markerOption.position(points.get(i)); markerOption.icon( BitmapDescriptorFactory.fromBitmap(BitmapFactory .decodeResource(getResources(), R.drawable.zwd_xiaodian))); markerOption.anchor(0.5f, 0.5f); Marker marker = aMap.addMarker(markerOption); listMarker.add(marker); } showline(); startMove(activity,tv_refresh); } SmoothMoveMarker:高德提供的平滑移動的API points:存放得到的經緯度集合 listMarker:存放自定義Marker的集合 polylineList:存放折線物件的集合 showline();劃線方法 startMove(activity,tv_refresh);小車滑動方法 使用者可以自己設定每個經緯度顯示的圖片,這裡是設定了一個小黃點.
(2)對地圖畫線
/*劃線*/ private void showline() { addPolylineInPlayGround(); Log.e("tag", points.toString()); // 獲取軌跡座標點 LatLngBounds.Builder b = LatLngBounds.builder(); for (int i = 0; i < points.size(); i++) { b.include(points.get(i)); } LatLngBounds bounds = b.build(); aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100)); } /*新增線條*/ private Polyline polyline; private void addPolylineInPlayGround() { List list = points; polyline = aMap.addPolyline(new PolylineOptions().setCustomTexture(BitmapDescriptorFactory.fromResource(R.drawable.line)) .addAll(list) .useGradient(true) .width(5)); }
(3)開始移動方法
public void startMove(final LocationActivity activity, final TextView tv_reflesh) { final PolylineOptions options = new PolylineOptions(); LatLng drivePoint = points.get(0); smoothMarker.setDescriptor(BitmapDescriptorFactory.fromResource(R.drawable.bei)); Pair<Integer, LatLng> pair = SpatialRelationUtil.calShortestDistancePoint(points, drivePoint); points.set(pair.first, drivePoint); List<LatLng> subList = points.subList(pair.first, points.size()); // 設定滑動的軌跡左邊點 smoothMarker.setPoints(subList); smoothMarker.setMoveListener(new SmoothMoveMarker.MoveListener() { @Override public void move(final double distance) { activity.runOnUiThread(new Runnable() { @Override public void run() { smoothMarker.setTotalDuration(50); LatLng position= smoothMarker.getPosition(); aMap.moveCamera(CameraUpdateFactory.changeLatLng(position)); Polyline polyline = aMap.addPolyline(options.color(Color.RED) .add(position) .useGradient(true) .visible(true) .width(5)); polylineList.add(polyline); if (((int)distance) < 1 ){ cease(activity); tv_reflesh.setVisibility(View.VISIBLE); } } }); } }); smoothMarker.startSmoothMove(); aMap.moveCamera(CameraUpdateFactory.zoomTo(14)); LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();//存放所有點的經緯度 for (int i = 0; i < listMarker.size(); i++) { boundsBuilder.include(listMarker.get(i).getPosition());//把所有點都include進去(LatLng型別) } aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(boundsBuilder.build(), 500, 400, 15));//第二個引數為四周留空寬度 }
使用者可以根據自己需求設定時間,總時長,速度等,詳見參照高德API。這裡監聽平滑移動時返回剩餘距離,判斷小於1的時候
停止,並把回放按鈕顯示出來,在呼叫一次實現重新軌跡回放的功能。
(4) /*停止軌跡回放*/
public void cease(LocationActivity activity) { Log.e("zoule","走了嗎"); polyline.remove(); for (Polyline polyline1 : polylineList) { polyline1.remove(); } polylineList.clear(); smoothMarker.destroy(); for(Marker marker: listMarker){ marker.remove(); } points.clear(); }
清理Marker,折線集合,到此基本功能算是有了,謝謝大家觀看.