1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| private Polyline realtimePolyline; private Track finalizedTrack; private List<LatLng> allPoints = new ArrayList<>(); private int[] allHeights = new int[0];
private int heightCounter = 1000; private Marker aircraftMarker; private Marker aircraftControllMarker; private LocationClient locationClient; /** * 添加点 高度 进入地图 * @param newPoint * @param height */ public void addPointCombined(LatLng newPoint, int height) {
// 添加新数据 allPoints.add(newPoint);
// 更新高度数组 int[] newHeights = Arrays.copyOf(allHeights, allHeights.length + 1); newHeights[allHeights.length] = height; allHeights = newHeights;
runOnUiThread(new Runnable() { @Override public void run() { //更新点 if (aircraftMarker == null) { // 第一次创建标记 MarkerOptions markerOptions = new MarkerOptions() .position(newPoint) .title("飞机位置") .icon(BitmapDescriptorFactory.fromResource(R.mipmap.icon_plane)) // 你的飞机图标 .anchor(0.5f, 0.5f); aircraftMarker = (Marker) baiduMap.addOverlay(markerOptions); } else { // 后续更新位置 aircraftMarker.setPosition(newPoint); }
// 更新实时线段 if(allPoints.size()>=2){ if (realtimePolyline == null) { PolylineOptions options = new PolylineOptions() .points(allPoints) .width(8) .color(0xAA0000FF); realtimePolyline = (Polyline) baiduMap.addOverlay(options); } else { realtimePolyline.setPoints(allPoints); } }
// 每20个点生成一次3D轨迹 if (allPoints.size() % 20 == 0) { update3DTrack(); }
// 调整视角,移动到中心位置,测试先关闭 // MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(newPoint); // baiduMap.animateMapStatus(mapStatusUpdate); } });
}
/** * 更新3D路线 */ private void update3DTrack() { if (finalizedTrack != null) { finalizedTrack.remove(); }
// ... 自定义绘制颜色渐变到paletteBitmap ... BitmapDescriptor paletteDesc = BitmapDescriptorFactory.fromResource(R.mipmap.ic_color); // 方法1:从资源文件创建(推荐) BitmapDescriptor projPalette = BitmapDescriptorFactory.fromResource(R.mipmap.ic_color);
BMTrackOptions options = new BMTrackOptions(); // options.setTrackType(BMTrackType.Surface); options.setTrackType(BMTrackType.Default3D); options.setPoints(allPoints); options.setHeights(allHeights); options.setPalette(createLineTexture(Color.parseColor("#4285F4"), 3));//一条显示,中间不显示 int[] gradientColors = {Color.RED, Color.YELLOW, Color.GREEN}; options.setOpacity(1f); options.setPaletteOpacity(0.1f); // 透明度 options.setProjectionPalette(projPalette); options.setWidth(10); // 轨迹宽度 options.setAnimationTime(100); // 动画时长(毫秒) options.setTraceAnimationListener(mTraceAnimationListener); // 其他设置...
finalizedTrack = (Track) baiduMap.addOverlay(options);
}
/** * 动画监听 */ private TraceAnimationListener mTraceAnimationListener = new TraceAnimationListener() {
@Override public void onTraceAnimationUpdate(float v) { // LogUtils.d("onTraceAnimationUpdate:"+v); }
@Override public void onTraceUpdatePosition(com.baidu.mapapi.model.LatLng latLng) { // LogUtils.d("onTraceUpdatePosition:"+latLng.toString()); }
@Override public void onTraceAnimationFinish() { // LogUtils.d("onTraceAnimationFinish:"); } };
//轨迹颜色start
private BitmapDescriptor createLineTexture(int color, int width) { // 创建1xN像素的Bitmap(N=线宽) // 创建一个1xwidth像素的纹理图 Bitmap bitmap = Bitmap.createBitmap(width, 1, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setColor(color); paint.setStrokeWidth(width); canvas.drawLine(0, 0, width, 0, paint);
return BitmapDescriptorFactory.fromBitmap(bitmap); }
//轨迹颜色end
|