地图轨迹内插采集点
有时候地图轨迹采集点不均匀,在做轨迹回放时速度很不均匀,忽快忽慢,通过内插采集点可以平滑播放,原理是把轨迹中每条直线分割成若干等长的线段,代码如下
function interpolation(points){ const options={units: 'kilometers'} //线转点集合,结果放到points中 const line2points=(p1,p2,distance,points)=>{ if(turf.distance(p1,p2,options)>distance){ const along=turf.along(turf.lineString([p1.geometry.coordinates,p2.geometry.coordinates]),distance,options) points.push(along) line2points(along,p2,distance,points) return; } points.push(p2); } //获取points相邻点间最短距离 const getMinDistance=(points)=>{ // const distances=[] // points.reduce((prev,curr)=>{distances.push(turf.distance(prev,curr,options));return curr;}) // return Math.min(...distances.filter(distance=>distance>0)) return 0.01 } const minDistance=getMinDistance(points) //在points所有相邻点间进行插入点操作 const result=[] points.reduce((prev,curr)=>{ const points=[] line2points(prev,curr,minDistance,points) result=[...result,...points]; return curr; }) return result }