Android 7.4.0 起支持动态轨迹绘制,开发者可以提供轨迹数据,来动态展示轨迹。支持监听轨迹绘制状态。
示例代码如下:
TraceOptions traceOptions = new TraceOptions();traceOptions.animationTime(5000);traceOptions.animate(true);traceOptions.animationType(TraceOptions.TraceAnimateType.TraceOverlayAnimationEasingCurveLinear);traceOptions.color(0xAAFF0000);traceOptions.width(10);traceOptions.points(getTraceLocation());
示例代码如下:
mBaiduMap.addTraceOverlay(traceOptions, new TraceAnimationListener() {@Overridepublic void onTraceAnimationUpdate(int percent) {// 轨迹动画更新进度回调}@Overridepublic void onTraceUpdatePosition(LatLng position) {// 轨迹动画更新的当前位置点回调}@Overridepublic void onTraceAnimationFinish() {// 轨迹动画结束回调}});
绘制效果如图:

自V7.5.4起,支持轨迹动画图标。开发者可自定义动画图标,图标跟随动态轨迹平滑移动。
示例代码如下:
// 创建轨迹对象TraceOptions traceOptions = initTraceOptions();// 创建图标BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.marker_blue);// 设置轨迹动画图标并让图标平滑移动traceOptions.icon(bitmap).setPointMove(true);// 添加轨迹动画mTraceOverlay = mBaiduMap.addTraceOverlay(traceOptions, this);
注:必须调用setPointMove(true)方法才能看到轨迹动画图标。
绘制效果如图:
自v7.5.7版本起,动态轨迹支持为每个轨迹点配置颜色来实现渐变轨迹效果。通过TraceOptions的useColorArray方法开启,并传入int类型的十六进制颜色数组。(例如 0xFF0000FF 表示不透明度为255的蓝色,从左至右每两位依次代表 a、r、g、b)
注:开启渐变效果后,轨迹线的抽稀和平滑效果将自动关闭。
代码如下:
TraceOptions traceOptions = initTraceOptions();...traceOptions.useColorArray(true);traceOptions.colors(arrColor);/*** 模拟轨迹点*/private static final LatLng[] latlngs = new LatLng[]{new LatLng(40.055826, 116.307917), new LatLng(40.055916, 116.308455), new LatLng(40.055967, 116.308549),new LatLng(40.056014, 116.308574), new LatLng(40.056440, 116.308485), new LatLng(40.056816, 116.308352),new LatLng(40.057997, 116.307725), new LatLng(40.058022, 116.307693), new LatLng(40.058029, 116.307590),new LatLng(40.057913, 116.307119), new LatLng(40.057850, 116.306945), new LatLng(40.057756, 116.306915),new LatLng(40.057225, 116.307164), new LatLng(40.056134, 116.307546), new LatLng(40.055879, 116.307636),new LatLng(40.055826, 116.307697),};/*** 每个元素对应一个轨迹点的颜色* 注:轨迹点和颜色的数量必须保持一致*/private static final int[] arrColor = new int[]{0xAA0000FF,0xAA00FF00,0xAAFF0000,0xAA0000FF,0xAA00FF00,0xAAFF0000,0xAA0000FF,0xAA00FF00,0xAAFF0000,0xAA0000FF,0xAA00FF00,0xAAFF0000,0xAA0000FF,0xAA0000FF,0xAA00FF00,0xAAFF0000,};
效果如图:

动态轨迹支持轨迹线的发光效果,开发者可通过TraceOptions的setTrackBloom方法开启,并通过setBloomSpeed控制轨迹的发光宽度。
代码如下:
/*** 设置轨迹线发光* @param isTrackBloom* @return*/public TraceOptions setTrackBloom(boolean isTrackBloom)/*** 轨迹发光宽度参数(取值范围 1.0f~10.0f,默认 5.0f)* @param bloomSpeed* @return*/public TraceOptions setBloomSpeed(float bloomSpeed)
效果如图:

动态轨迹支持使用3D模型作为轨迹图标,开发者可通过TraceOptions的icon3D方法开启,目前支持 .obj 和 .gltf 格式模型,其中 .gltf 模型支持播放动画。
代码如下:
TraceOptions traceOptions = initTraceOptions();...traceOptions.setPointMove(true);traceOptions.icon3D(init3DModelOptions());/*** 配置3D模型参数,暂时只支持 .obj/.gltf 格式*/private BM3DModelOptions init3DModelOptions(){// 3D模型轨迹点只支持如下设置BM3DModelOptions bm3DModelOptions = new BM3DModelOptions();// 路径和名称为必填项bm3DModelOptions.setModelPath(parentPath + "/model3D");bm3DModelOptions.setScale(20.0f);// bm3DModelOptions.setZoomFixed(false);//文件后缀名也需要一起传入bm3DModelOptions.setModelName("CesiumMan.gltf");bm3DModelOptions.setBM3DModelType(BM3DModelOptions.BM3DModelType.BM3DModelTypeglTF);bm3DModelOptions.setSkeletonAnimationEnable(true);bm3DModelOptions.animationSpeed(2.0f);bm3DModelOptions.animationRepeatCount(0);//调整模型位置bm3DModelOptions.setYawAxis(BM3DModelOptions.ModelYawAxis.Z);bm3DModelOptions.setRotate(0f,0f,0.0f);bm3DModelOptions.setOffset(0.0f,0.0f,-100.0f);return bm3DModelOptions;}
效果如下:
开发者可以通过TraceOverlay的pause()方法暂停轨迹播放,通过resume()方法恢复播放。
效果如下:
上一篇
下一篇