Search code examples
javascriptleafletarcgisesri

How do I prevent a dynamicMapLayer from refreshing on every zoom or pan of the map?


I'm using the leaflet L.esri.dynamicMapLayer to display a large amount of polylines on the map, and the export request to ArcGIS Server to draw them can take a while. If the user quickly makes several zooms or pans, I can end up with a bunch of pending export requests, which also blocks other requests to ArcGIS Server. All those export requests except the last are useless.

For other client side layers, I'm already controlling the refresh by making sure the user has stopped zooming or panning for at least 2 second before refreshing the layers myself. How can I do the same for the dynamicMapLayer, can I pause or stop the automatic refresh and decide myself when I want the export request to be made?

Note that we cannot use tiles for better performance, because of other reasons the layer must stay dynamic.


Solution

  • How do I prevent a dynamicMapLayer from refreshing on every zoom or pan of the map?

    You can not. It is designed that way.

    Unless the esri folks redesign that to make a subclass of L.GridLayer instead of L.ImageOverlay, there's hardly any way around it.

    I'm already controlling the refresh by making sure the user has stopped zooming or panning for at least 2 second before refreshing the layers myself. How can I do the same for the dynamicMapLayer?

    With a horrible, ugly hack. Overwrite the private L.Esri.DynamicMapLayer._update method so that it becomes a decorator over the previous method, e.g. something like:

    (function() {
      var previousProto = L.Esri.DynamicMapLayer.prototype;
      L.Esri.DynamicMapLayer.include({
        _update: function(){
           throttle(previousProto._update, 2000);
        }
      });
    })();
    

    It's ugly, it's against most good coding practices (overwriting private methods, eeeew), and it might break.

    Note that we cannot use tiles for better performance, because of other reasons the layer must stay dynamic.

    I disagree. "tiles" doesn't mean "static". You can easily apply cache-busting, or use a time dimension, or send all data to the client and let it slice it into vector tiles for quick rendering, or use something fancier like Carto(DB)'s Torque.

    The fact that your Esri tools don't allow you to readily create different sets of tiles, or do not allow for tiled access of changing resources, or do not allow for triggering client-side data invalidations, doesn't mean that it cannot be accomplished.