当前位置:网站首页>web技术分享| 【高德地图】实现自定义的轨迹回放
web技术分享| 【高德地图】实现自定义的轨迹回放
2022-06-22 15:25:00 【InfoQ】

实现(轨迹回放)方式有两种:
- 第一种是使用
JS API和AMap.PolyLine(折线)等图形配合实现。
- 第二种是使用
JS API和AMapUI 组件库配合使用,利用PathSimplifier(轨迹展示组件)绘制出行动轨迹。
方案选择
实现流程
- 客户端按(时间)周期上报地理信息以及自定义信息。
- 服务端按时间轴存储客户上报的信息。
- 按(时间等)条件查询出用户的轨迹,并通过简化算法去除一部分节点(例如,节点距离十分微小、或者多个点都在同一条直线、3点之间,其中一点略有偏差无法绘制成直线等等),最终获得适合绘制的路径(数组)。
- 根据路径去绘制用户的行动轨迹。
路径简化算法(可选)
A点和B点,两者距离不到1像素,则可以去掉B点,只留A点。
A,B,C三点在一条直线上,或者,B点仅仅稍微偏离A点和C点构成的线段,那么B点就可以去掉。
simplify.js实现示例
车辆轨迹回放
JS APIAMap.PolyLine
实现原理:
- 在地图上绘制车辆标记(
AMap.Marker)。
- 利用
AMap.PolyLine绘制出两条轨迹:历史轨迹和驾驶途径过的轨迹,以颜色区分。
- 按照一定的速度使车辆前进,并监听
Maker移动的事件,在事件回调中,将车辆(Marker)位置设置为地图中心点,给使用者视觉主观上一种车辆在前进的感觉,同时延长驾驶途径过的轨迹。
- 对于实现场景比较复杂的,需要进行自定义处理的比如:
- 查看每个节点的数据,我们可以把每个节点给绘制出来,节点被点击时显示该节点的数据。
- 移动倍速播放,首先按上报的时间间隔来播放,选择倍速之后,改变
MarKer移动的duration。
- 其他自定义。
自定义 API
- 开始移动
- 暂停移动
- 恢复移动
- 停止移动
代码示例
AMap.plugin('AMap.MoveAnimation', function(){
var marker, lineArr = [[116.478935,39.997761],[116.478939,39.997825],[116.478912,39.998549],[116.478912,39.998549],[116.478998,39.998555],[116.478998,39.998555],[116.479282,39.99856],[116.479658,39.998528],[116.480151,39.998453],[116.480784,39.998302],[116.480784,39.998302],[116.481149,39.998184],[116.481573,39.997997],[116.481863,39.997846],[116.482072,39.997718],[116.482362,39.997718],[116.483633,39.998935],[116.48367,39.998968],[116.484648,39.999861]];
var map = new AMap.Map("container", {
resizeEnable: true,
center: [116.397428, 39.90923],
zoom: 17
});
marker = new AMap.Marker({
map: map,
position: [116.478935,39.997761],
icon: "https://a.amap.com/jsapi_demos/static/demo-center-v2/car.png",
offset: new AMap.Pixel(-13, -26),
});
// 绘制历史轨迹
var polyline = new AMap.Polyline({
map: map,
path: lineArr,
showDir:true,
strokeColor: "#28F", //线颜色
// strokeOpacity: 1, //线透明度
strokeWeight: 6, //线宽
// strokeStyle: "solid" //线样式
});
// 驾驶途径过的轨迹
var passedPolyline = new AMap.Polyline({
map: map,
strokeColor: "#AF5", //线颜色
strokeWeight: 6, //线宽
});
// 监听车辆移动事件
marker.on('moving', function (e) {
// 延长驾驶途径过的轨迹
passedPolyline.setPath(e.passedPath);
// 将车辆位置设置为地图中心点
map.setCenter(e.target.getPosition(),true)
});
map.setFitView();
// 开始移动
window.startAnimation = function startAnimation () {
marker.moveAlong(lineArr, {
// 每一段的时长
duration: 500,//可根据实际采集时间间隔设置
// JSAPI2.0 是否延道路自动设置角度在 moveAlong 里设置
autoRotation: true,
});
};
// 暂停移动
window.pauseAnimation = function () {
marker.pauseMove();
};
// 恢复移动
window.resumeAnimation = function () {
marker.resumeMove();
};
// 停止移动
window.stopAnimation = function () {
marker.stopMove();
};
});
飞机航班的轨迹回放
JS APIAMapUI 组件库PathSimplifier实现原理:
- 在地图上绘制飞机标记(
AMap.Marker)。
- 利用
AMap.PolyLine绘制出两条轨迹:历史轨迹和驾驶途径过的轨迹,以颜色区分。
- 配置轨迹的颜色,动画的速度等等。
- 对于实现场景比较复杂的,需要进行自定义处理的,可以在
PathSimplifier提供的回调中进行配置及处理。
示例代码
//加载PathSimplifier,loadUI的路径参数为模块名中 'ui/' 之后的部分
AMapUI.load(['ui/misc/PathSimplifier'], function(PathSimplifier) {
if (!PathSimplifier.supportCanvas) {
alert('当前环境不支持 Canvas!');
return;
}
//启动页面
initPage(PathSimplifier);
});
function initPage(PathSimplifier) {
//创建组件实例
var pathSimplifierIns = new PathSimplifier({
zIndex: 100,
map: map, //所属的地图实例
getPath: function(pathData, pathIndex) {
//返回轨迹数据中的节点坐标信息,[AMap.LngLat, AMap.LngLat...] 或者 [[lng|number,lat|number],...]
return pathData.path;
},
getHoverTitle: function(pathData, pathIndex, pointIndex) {
//返回鼠标悬停时显示的信息
if (pointIndex >= 0) {
//鼠标悬停在某个轨迹节点上
return pathData.name + ',点:' + pointIndex + '/' + pathData.path.length;
}
//鼠标悬停在节点之间的连线上
return pathData.name + ',点数量' + pathData.path.length;
},
renderOptions: {
//轨迹线的样式
pathLineStyle: {
strokeStyle: 'red',
lineWidth: 6,
dirArrowStyle: true
}
}
});
//这里构建两条简单的轨迹,仅作示例
pathSimplifierIns.setData([{
name: '轨迹0',
path: [
[100.340417, 27.376994],
[108.426354, 37.827452],
[113.392174, 31.208439],
[124.905846, 42.232876]
]
}, {
name: '大地线',
//创建一条包括500个插值点的大地线
path: PathSimplifier.getGeodesicPath([116.405289, 39.904987], [87.61792, 43.793308], 500)
}]);
//创建一个巡航器
var navg0 = pathSimplifierIns.createPathNavigator(0, //关联第1条轨迹
{
loop: true, //循环播放
speed: 1000000
});
navg0.start();
}

边栏推荐
- nio使用可写事件处理一次性写不完情况
- Spark性能调优之道——解决Spark数据倾斜(Data Skew)的N种姿势
- [pop up box 2 at the bottom of wechat applet package]
- Basic knowledge of audio and video | analysis of ANS noise suppression principle
- SAP ABAP data dictionary tutorial se11: tables, locked objects, views, and structures-03
- How to use IDM to accelerate Baidu cloud
- 代码扫描工具扫出的 Arrays.asList 使用BUG
- 面试题之 <img>标签 的 title 和 alt 有什么区别
- 洞见科技牵头的全球「首个」IEEE隐私计算「互联互通」国际标准正式启动
- 全球首款AR隐形眼镜,元宇宙入口这次真的打开了?
猜你喜欢

JS获取数据类型方法总结

SAP ABAP internal tables: create, read, populate, copy and delete-06

In case of default import failure

视频爆炸时代,谁在支撑视频生态网高速运行?

【小程序项目开发-- 京东商城】uni-app开发之配置tabBar & 窗口样式

机器学习笔记 - HaGRID—手势识别图像数据集简介

jsp学习之(一)---------jsp概述
![[deep anatomy of C language] keywords if & else & bool type](/img/cf/a0533b7d3a597368aefe6ce7fd6dbb.png)
[deep anatomy of C language] keywords if & else & bool type

Summary of JS methods for obtaining data types

'不敢去怀疑代码,又不得不怀疑代码'记一次网络请求超时分析
随机推荐
scala的相等性
Summary of JS methods for obtaining data types
spark的NaiveBayes中文文本分类
什么是RESTful,REST api设计时应该遵守什么样的规则?
What is SAP ABAP? Type, ABAP full form and meaning
Learning about ABAP program tuning (IV) loop where key
Add a millennial sign to a number (amount in millennia)
Summary of Changan chain usage skills
Simple understanding of asynchronous IO
NiO uses writable events to handle the situation of one-time write incompleteness
IO模型的5中模式
使用枚举实现工厂模式
【C语言】深度剖析整型和浮点型在内存中的存储
3. Classe abstraite (forme)
接口(优化类型注解)
超出文本部分用省略号表示
Implementing factory mode using enumeration
SAP ABAP sub screen tutorial: call sub screen -010 in SAP
ALV report in SAP tutorial - ABAP list viewer -012
Implementation classes with similar execution logic use the template pattern