之前有一篇博客简单写了一个模拟demo,根据物体当前的速度和方向预测多少时间后所在的位置,具体计算是参考《(译)计算距离、方位以及更多经纬度之间的点》,现在重新用计算公式实现,代码如下:
let from, to; let createVelLine = obj => { const DEG2RAD = Math.PI / 180; const RAD2DEG = 180 / Math.PI; const radius = 6371.0088; //km const KTS2KPH = 1.85200; let pred_secs = 30; //预测未来30s的轨迹线 //角度转弧度 let latFrom = obj.latitude * DEG2RAD; let lonFrom = obj.longitude * DEG2RAD; let bearing = obj.rotation; //30s后预测点的距离 if (map.getView().getZoom() < 8) pred_secs *= 4; let distance = obj.speed * KTS2KPH * (pred_secs / 3600); //公里 H //计算预测点坐标 let latTo = Math.asin( Math.sin(latFrom) * Math.cos(distance / radius) + Math.cos(latFrom) * Math.sin(distance / radius) * Math.cos(bearing)); let lonTo = lonFrom + Math.atan2( Math.sin(bearing) * Math.sin(distance / radius) * Math.cos(latFrom), Math.cos(distance / radius) - Math.sin(latFrom) * Math.sin(latTo) ); lonTo = ((lonTo + 3 * Math.PI) % (2 * Math.PI)) - Math.PI; //弧度转角度 let lat_dist = latTo * RAD2DEG; let lon_dist = lonTo * RAD2DEG; //转换成起点和终点坐标 from = ol.proj.fromLonLat([obj.longitude, obj.latitude]); to = ol.proj.fromLonLat([lon_dist, lat_dist]); }在更新数据的位置调用该函数传参即可。