TransWikia.com

OpenLayers clear path on marker movement

Geographic Information Systems Asked on January 2, 2021

I’m using OpenLayers 6.

I would like to produce behaviour like on Google Maps:

When the marker has passed the point, all previous points should be hidden and the only route that is left to pass should be visible.

For example:

https://openlayers.org/en/latest/examples/feature-move-animation.html

So when I start marker animation, I would like the previous yellow route that marker passed to be hidden, and only route ahead of him should be visible

Is that even possible?

One Answer

If you want to achieve desired effect of clearing the route behind the moving marker in mentioned example, all you have to do is to modify moveFeature function, so that at each marker move new route geometry is created that contains only points not covered yet by the moving marker.

Code could then look something like this (just three lines inserted):

var moveFeature = function (event) {
  var vectorContext = ol.render.getVectorContext(event);
  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(routeCoords[index]);
    var feature = new ol.Feature(currentPoint);
    vectorContext.drawFeature(feature, styles.geoMarker);

    // three new lines
    var remainingRouteCoords = routeCoords.slice(index);
    var remainingRouteGeom = new ol.geom.LineString(remainingRouteCoords);
    routeFeature.setGeometry(remainingRouteGeom);
  }
  map.render();
};

Answered by TomazicM on January 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP