140 lines
3.8 KiB
JavaScript
140 lines
3.8 KiB
JavaScript
class MarkerAnimator {
|
|
constructor(map) {
|
|
this.map = map;
|
|
this.animations = [];
|
|
this.running = false;
|
|
}
|
|
|
|
animate(marker, endLonLat, duration = 2000) {
|
|
const geometry = marker.getGeometry();
|
|
const start = geometry.getCoordinates();
|
|
const end = ol.proj.fromLonLat(endLonLat);
|
|
const startTime = performance.now();
|
|
|
|
this.animations.push({
|
|
marker,
|
|
start,
|
|
end,
|
|
startTime,
|
|
duration
|
|
});
|
|
|
|
if (!this.running) {
|
|
this.running = true;
|
|
requestAnimationFrame(this.step.bind(this));
|
|
}
|
|
}
|
|
|
|
step(currentTime) {
|
|
let needsRender = false;
|
|
|
|
this.animations = this.animations.filter(anim => {
|
|
const { marker, start, end, startTime, duration } = anim;
|
|
const fraction = Math.min((currentTime - startTime) / duration, 1);
|
|
|
|
const interpolated = [
|
|
start[0] + fraction * (end[0] - start[0]),
|
|
start[1] + fraction * (end[1] - start[1]),
|
|
];
|
|
|
|
marker.getGeometry().setCoordinates(interpolated);
|
|
needsRender = true;
|
|
|
|
return fraction < 1;
|
|
});
|
|
|
|
if (needsRender) {
|
|
window.Map_.render(); // Only once per frame
|
|
}
|
|
|
|
if (this.animations.length > 0) {
|
|
requestAnimationFrame(this.step.bind(this));
|
|
} else {
|
|
this.running = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
class MarkerManager {
|
|
constructor(layer, styleFunction) {
|
|
this.layer = layer;
|
|
this.source = layer.getSource();
|
|
this.markers = {};
|
|
this.styleFunction = styleFunction;
|
|
}
|
|
|
|
addMarker(id, coordinates, properties = {}) {
|
|
const point = new ol.geom.Point(ol.proj.fromLonLat(coordinates));
|
|
const feature = new ol.Feature(point);
|
|
|
|
for (const prop in properties) {
|
|
feature.set(prop, properties[prop]);
|
|
}
|
|
|
|
if (this.styleFunction) {
|
|
feature.setStyle(this.styleFunction(feature));
|
|
}
|
|
|
|
this.source.addFeature(feature);
|
|
this.markers[id] = feature;
|
|
return feature;
|
|
}
|
|
|
|
updateMarker(id, coordinates, properties = {}) {
|
|
const marker = this.markers[id];
|
|
if (!marker) return this.addMarker(id, coordinates, properties);
|
|
|
|
const newCoords = ol.proj.fromLonLat(coordinates);
|
|
marker.getGeometry().setCoordinates(newCoords);
|
|
|
|
for (const prop in properties) {
|
|
marker.set(prop, properties[prop]);
|
|
}
|
|
|
|
return marker;
|
|
}
|
|
|
|
removeMarker(marker) {
|
|
delete this.markers[marker.get('id')]
|
|
this.source.removeFeature(marker)
|
|
}
|
|
|
|
removeMarkers(markers) {
|
|
markers.forEach(marker => {
|
|
delete this.markers[marker.get('id')]
|
|
})
|
|
this.source.removeFeatures(markers)
|
|
}
|
|
|
|
animateMarker(id, newCoordinates, duration = 2000) {
|
|
const marker = this.markers[id];
|
|
if (!marker) return;
|
|
|
|
const startCoordinates = marker.getGeometry().getCoordinates();
|
|
const endCoordinates = ol.proj.fromLonLat(newCoordinates);
|
|
const startTime = performance.now();
|
|
|
|
const animate = (currentTime) => {
|
|
const elapsed = currentTime - startTime;
|
|
const fraction = Math.min(elapsed / duration, 1);
|
|
|
|
const interpolated = [
|
|
startCoordinates[0] + fraction * (endCoordinates[0] - startCoordinates[0]),
|
|
startCoordinates[1] + fraction * (endCoordinates[1] - startCoordinates[1])
|
|
];
|
|
|
|
marker.getGeometry().setCoordinates(interpolated);
|
|
|
|
if (fraction < 1) {
|
|
requestAnimationFrame(animate);
|
|
}
|
|
};
|
|
|
|
requestAnimationFrame(animate);
|
|
}
|
|
|
|
clearMarkers() {
|
|
this.source.clear()
|
|
}
|
|
}
|