openlayers4_根据点模拟轨迹运动
作者:互联网
openlayers4模拟轨迹运动
1. 切割一段线,返回点数组
function cfLineString(start, end, line) {
var c = line.getLength() * 100//千米
var count = Math.round(c);
var routeCoords = [];
var startx = start[0];
var starty = start[1];
var endx = end[0];
var endy = end[1];
var avg_x = (endx - startx) / count;
var avg_y = (endy - starty) / count;
routeCoords.push(start);
var val_x = startx;
var val_y = starty;
for (var i = 0; i < count; i++) {
val_x += avg_x;
val_y += avg_y;
var val = [val_x, val_y];
routeCoords.push(val);
}
routeCoords.push(end);
return routeCoords;
}
2.模拟轨迹
// 开始动画
function startAnimation() {
if (animating) {
stopAnimation(false);
} else {
animating = true;
now = new Date().getTime();
speed = 350;
geoMarker.setStyle(null);
map.on('postcompose', moveFeature);
map.render();
}
}
// 停止动画
function stopAnimation(ended) {
animating = false;
var coord = ended ? cfPoints[routeLength - 1] : cfPoints[0];
(geoMarker.getGeometry()).setCoordinates(coord);
map.un('postcompose', moveFeature);
}
// 监听事件
var moveFeature = function (event) {
var vectorContext = event.vectorContext;
var frameState = event.frameState;
if (animating) {
var elapsedTime = frameState.time - now;
var index = Math.round(speed * elapsedTime / 1000);
if (index >= routeLength) {
stopAnimation(true);
return;
}
var currentPoint = new ol.geom.Point(cfPoints[index]);
var feature = new ol.Feature(currentPoint);
vectorContext.drawFeature(feature, markerStyle);
}
map.render();
};
标签:轨迹,end,openlayers4,val,routeCoords,start,var,avg,模拟 来源: https://www.cnblogs.com/yyxbot/p/16177445.html