Fetch the polyline and smoother animation
Fetch the polyline and use getCoordinateAt() for smooth animation
This commit is contained in:
@@ -15,124 +15,6 @@ import {
|
||||
import {Tile as TileLayer, Vector as VectorLayer} from '../src/ol/layer.js';
|
||||
import {getVectorContext} from '../src/ol/render.js';
|
||||
|
||||
// This long string is placed here due to jsFiddle limitations.
|
||||
// It is usually loaded with AJAX.
|
||||
const polyline = [
|
||||
'hldhx@lnau`BCG_EaC??cFjAwDjF??uBlKMd@}@z@??aC^yk@z_@se@b[wFdE??wFfE}N',
|
||||
'fIoGxB_I\\gG}@eHoCyTmPqGaBaHOoD\\??yVrGotA|N??o[N_STiwAtEmHGeHcAkiA}^',
|
||||
'aMyBiHOkFNoI`CcVvM??gG^gF_@iJwC??eCcA]OoL}DwFyCaCgCcCwDcGwHsSoX??wI_E',
|
||||
'kUFmq@hBiOqBgTwS??iYse@gYq\\cp@ce@{vA}s@csJqaE}{@iRaqE{lBeRoIwd@_T{]_',
|
||||
'Ngn@{PmhEwaA{SeF_u@kQuyAw]wQeEgtAsZ}LiCarAkVwI}D??_}RcjEinPspDwSqCgs@',
|
||||
'sPua@_OkXaMeT_Nwk@ob@gV}TiYs[uTwXoNmT{Uyb@wNg]{Nqa@oDgNeJu_@_G}YsFw]k',
|
||||
'DuZyDmm@i_@uyIJe~@jCg|@nGiv@zUi_BfNqaAvIow@dEed@dCcf@r@qz@Egs@{Acu@mC',
|
||||
'um@yIey@gGig@cK_m@aSku@qRil@we@{mAeTej@}Tkz@cLgr@aHko@qOmcEaJw~C{w@ka',
|
||||
'i@qBchBq@kmBS{kDnBscBnFu_Dbc@_~QHeU`IuyDrC_}@bByp@fCyoA?qMbD}{AIkeAgB',
|
||||
'k_A_A{UsDke@gFej@qH{o@qGgb@qH{`@mMgm@uQus@kL{_@yOmd@ymBgwE}x@ouBwtA__',
|
||||
'DuhEgaKuWct@gp@cnBii@mlBa_@}|Asj@qrCg^eaC}L{dAaJ_aAiOyjByH{nAuYu`GsAw',
|
||||
'Xyn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||||
|
||||
const route = /** @type {import("../src/ol/geom/LineString.js").default} */ (new Polyline(
|
||||
{
|
||||
factor: 1e6,
|
||||
}
|
||||
).readGeometry(polyline, {
|
||||
dataProjection: 'EPSG:4326',
|
||||
featureProjection: 'EPSG:3857',
|
||||
}));
|
||||
|
||||
const routeCoords = route.getCoordinates();
|
||||
const routeLength = routeCoords.length;
|
||||
|
||||
const routeFeature = new Feature({
|
||||
type: 'route',
|
||||
geometry: route,
|
||||
});
|
||||
const geoMarker = /** @type Feature<import("../src/ol/geom/Point").default> */ (new Feature(
|
||||
{
|
||||
type: 'geoMarker',
|
||||
geometry: new Point(routeCoords[0]),
|
||||
}
|
||||
));
|
||||
const startMarker = new Feature({
|
||||
type: 'icon',
|
||||
geometry: new Point(routeCoords[0]),
|
||||
});
|
||||
const endMarker = new Feature({
|
||||
type: 'icon',
|
||||
geometry: new Point(routeCoords[routeLength - 1]),
|
||||
});
|
||||
|
||||
const styles = {
|
||||
'route': new Style({
|
||||
stroke: new Stroke({
|
||||
width: 6,
|
||||
color: [237, 212, 0, 0.8],
|
||||
}),
|
||||
}),
|
||||
'icon': new Style({
|
||||
image: new Icon({
|
||||
anchor: [0.5, 1],
|
||||
src: 'data/icon.png',
|
||||
}),
|
||||
}),
|
||||
'geoMarker': new Style({
|
||||
image: new CircleStyle({
|
||||
radius: 7,
|
||||
fill: new Fill({color: 'black'}),
|
||||
stroke: new Stroke({
|
||||
color: 'white',
|
||||
width: 2,
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
};
|
||||
|
||||
let animating = false;
|
||||
let speed, now;
|
||||
const speedInput = document.getElementById('speed');
|
||||
const startButton = document.getElementById('start-animation');
|
||||
|
||||
const vectorLayer = new VectorLayer({
|
||||
source: new VectorSource({
|
||||
features: [routeFeature, geoMarker, startMarker, endMarker],
|
||||
}),
|
||||
style: function (feature) {
|
||||
// hide geoMarker if animation is active
|
||||
if (animating && feature.get('type') === 'geoMarker') {
|
||||
return null;
|
||||
}
|
||||
return styles[feature.get('type')];
|
||||
},
|
||||
});
|
||||
|
||||
const key = 'get_your_own_D6rA4zTHduk6KOKTXzGB';
|
||||
const attributions =
|
||||
'<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a> ' +
|
||||
@@ -155,63 +37,134 @@ const map = new Map({
|
||||
tileSize: 512,
|
||||
}),
|
||||
}),
|
||||
vectorLayer,
|
||||
],
|
||||
});
|
||||
|
||||
const moveFeature = function (event) {
|
||||
const vectorContext = getVectorContext(event);
|
||||
const frameState = event.frameState;
|
||||
// The polyline string is read from a JSON similiar to those returned
|
||||
// by directions APIs such as Openrouteservice and Mapbox.
|
||||
fetch('data/polyline/route.json').then(function (response) {
|
||||
response.json().then(function (result) {
|
||||
const polyline = result.routes[0].geometry;
|
||||
|
||||
if (animating) {
|
||||
const elapsedTime = frameState.time - now;
|
||||
// here the trick to increase speed is to jump some indexes
|
||||
// on lineString coordinates
|
||||
const index = Math.round((speed * elapsedTime) / 1000);
|
||||
const route = new Polyline({
|
||||
factor: 1e6,
|
||||
}).readGeometry(polyline, {
|
||||
dataProjection: 'EPSG:4326',
|
||||
featureProjection: 'EPSG:3857',
|
||||
});
|
||||
|
||||
if (index >= routeLength) {
|
||||
stopAnimation(true);
|
||||
return;
|
||||
const routeFeature = new Feature({
|
||||
type: 'route',
|
||||
geometry: route,
|
||||
});
|
||||
const geoMarker = new Feature({
|
||||
type: 'geoMarker',
|
||||
geometry: new Point(route.getCoordinateAt(0)),
|
||||
});
|
||||
const startMarker = new Feature({
|
||||
type: 'icon',
|
||||
geometry: new Point(route.getCoordinateAt(0)),
|
||||
});
|
||||
const endMarker = new Feature({
|
||||
type: 'icon',
|
||||
geometry: new Point(route.getCoordinateAt(1)),
|
||||
});
|
||||
|
||||
const styles = {
|
||||
'route': new Style({
|
||||
stroke: new Stroke({
|
||||
width: 6,
|
||||
color: [237, 212, 0, 0.8],
|
||||
}),
|
||||
}),
|
||||
'icon': new Style({
|
||||
image: new Icon({
|
||||
anchor: [0.5, 1],
|
||||
src: 'data/icon.png',
|
||||
}),
|
||||
}),
|
||||
'geoMarker': new Style({
|
||||
image: new CircleStyle({
|
||||
radius: 7,
|
||||
fill: new Fill({color: 'black'}),
|
||||
stroke: new Stroke({
|
||||
color: 'white',
|
||||
width: 2,
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
};
|
||||
|
||||
let animating = false;
|
||||
|
||||
const vectorLayer = new VectorLayer({
|
||||
source: new VectorSource({
|
||||
features: [routeFeature, geoMarker, startMarker, endMarker],
|
||||
}),
|
||||
style: function (feature) {
|
||||
// hide geoMarker if animation is active
|
||||
if (animating && feature.get('type') === 'geoMarker') {
|
||||
return null;
|
||||
}
|
||||
return styles[feature.get('type')];
|
||||
},
|
||||
});
|
||||
|
||||
map.addLayer(vectorLayer);
|
||||
|
||||
let speed, startTime;
|
||||
const speedInput = document.getElementById('speed');
|
||||
const startButton = document.getElementById('start-animation');
|
||||
|
||||
function moveFeature(event) {
|
||||
const vectorContext = getVectorContext(event);
|
||||
const frameState = event.frameState;
|
||||
|
||||
if (animating) {
|
||||
const elapsedTime = frameState.time - startTime;
|
||||
const distance = (speed * elapsedTime) / 1e6;
|
||||
|
||||
if (distance >= 1) {
|
||||
stopAnimation(true);
|
||||
return;
|
||||
}
|
||||
|
||||
const currentPoint = new Point(route.getCoordinateAt(distance));
|
||||
const feature = new Feature(currentPoint);
|
||||
vectorContext.drawFeature(feature, styles.geoMarker);
|
||||
}
|
||||
// tell OpenLayers to continue the postrender animation
|
||||
map.render();
|
||||
}
|
||||
|
||||
const currentPoint = new Point(routeCoords[index]);
|
||||
const feature = new Feature(currentPoint);
|
||||
vectorContext.drawFeature(feature, styles.geoMarker);
|
||||
}
|
||||
// tell OpenLayers to continue the postrender animation
|
||||
map.render();
|
||||
};
|
||||
function startAnimation() {
|
||||
if (animating) {
|
||||
stopAnimation(false);
|
||||
} else {
|
||||
animating = true;
|
||||
startTime = new Date().getTime();
|
||||
speed = speedInput.value;
|
||||
startButton.textContent = 'Cancel Animation';
|
||||
// hide geoMarker
|
||||
geoMarker.changed();
|
||||
// just in case you pan somewhere else
|
||||
map.getView().setCenter(center);
|
||||
vectorLayer.on('postrender', moveFeature);
|
||||
map.render();
|
||||
}
|
||||
}
|
||||
|
||||
function startAnimation() {
|
||||
if (animating) {
|
||||
stopAnimation(false);
|
||||
} else {
|
||||
animating = true;
|
||||
now = new Date().getTime();
|
||||
speed = speedInput.value;
|
||||
startButton.textContent = 'Cancel Animation';
|
||||
// hide geoMarker
|
||||
geoMarker.setStyle(null);
|
||||
// just in case you pan somewhere else
|
||||
map.getView().setCenter(center);
|
||||
vectorLayer.on('postrender', moveFeature);
|
||||
map.render();
|
||||
}
|
||||
}
|
||||
function stopAnimation(ended) {
|
||||
animating = false;
|
||||
startButton.textContent = 'Start Animation';
|
||||
|
||||
/**
|
||||
* @param {boolean} ended end of animation.
|
||||
*/
|
||||
function stopAnimation(ended) {
|
||||
animating = false;
|
||||
startButton.textContent = 'Start Animation';
|
||||
// if animation cancelled set the marker at the beginning
|
||||
const coord = route.getCoordinateAt(ended ? 1 : 0);
|
||||
geoMarker.getGeometry().setCoordinates(coord);
|
||||
// remove listener
|
||||
vectorLayer.un('postrender', moveFeature);
|
||||
}
|
||||
|
||||
// if animation cancelled set the marker at the beginning
|
||||
const coord = ended ? routeCoords[routeLength - 1] : routeCoords[0];
|
||||
const geometry = geoMarker.getGeometry();
|
||||
geometry.setCoordinates(coord);
|
||||
//remove listener
|
||||
vectorLayer.un('postrender', moveFeature);
|
||||
}
|
||||
|
||||
startButton.addEventListener('click', startAnimation, false);
|
||||
startButton.addEventListener('click', startAnimation, false);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user