Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

The ArcGIS Javascript API has a nice refresh interface for ArcGISDynamicMapServiceLayer:

    var dynamicMapServiceLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer");
    map.addLayer(dynamicMapServiceLayer);
    dynamicMapServiceLayer.setDisableClientCaching(true);

    if (dynamicMapServiceLayer.disableClientCaching) {
        aspect.after(dynamicMapServiceLayer, "onUpdateEnd", function () {
            $.delay(15000, "UpdateDynamicLayer", function () {
                dynamicMapServiceLayer.refresh();
            });
        });
    }

But this is not generalized across the other layer types. Anyone have a solution for refreshing layers of any type?

share|improve this question
    
Can you outline a scenario where you'd need to refresh a tiled or feature layer? Would removing a layer then re-adding it suffice? –  Stephen Lead Aug 30 '12 at 5:01
    
The code below does remove and re-add it and it does suffice to some degree...it's impossible to clone a layer so you need the constructor and it's not nearly as elegant as layer.refresh(). Regarding a scenario...updating weather radar. Not interested in tiles but in WMS. –  ca0v Aug 30 '12 at 13:33
    
Cross-posted this to the ESRI forum at forums.arcgis.com/threads/65819-Refreshing-WMS-layers –  ca0v Aug 30 '12 at 13:55
add comment

2 Answers

up vote 0 down vote accepted

Replace getImageUrl and modify the url before passing it to the callback:

    layer = new esri.layers.WMSLayer("http://sampleserver1c.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer", {
        visibleLayers: ['1', '2']
    });

    function extendLayer(layer)
    {
        var getImageUrl, tick;

        tick = 0;
        getImageUrl = layer.getImageUrl;

        layer.setDisableClientCaching = layer.setDisableClientCaching || function(disable)
        {
            if (disable)
            {
                layer.getImageUrl = function (extent, width, height, callback) {
                    var result = getImageUrl.call(layer, extent, width, height, function (url) {
                        callback.call(layer, url + "&_c=" + tick++);
                    });
                    return result;
                };
            }
            else
            {
                layer.getImageUrl = getImageUrl;
            }
        };
        return layer;
    }

    extendLayer ( layer ).setDisableClientCaching ( true );

    setInterval(function () {
        layer.refresh();
    }, 5000);

    map.addLayer(layer);
share|improve this answer
add comment

So far this is the best I've come up with for WMS layers:

    function addWmsLayer()
    {
        var layer, deferred;
        deferred = new Deferred ();
        layer = new esri.layers.WMSLayer (
            "http://sampleserver1c.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer",
            {
                transparent: true,
                imageFormat: "png"
            }
        );
        layer.setVisibleLayers([1]);
        aspect.after(layer, "onLoad", function () {
            var resourceInfo;
            resourceInfo = {
                extent : layer.extent,
                layerInfos : layer.layerInfos,
                version : layer.version,
                maxWidth : layer.maxWidth,
                maxHeight : layer.maxHeight,
                getMapUrl : layer.getMapURL,
                title : layer.title,
                description : layer.description,
                copyright : layer.copyright
            };
            deferred.resolve ( {
                layer : layer,
                resourceInfo : resourceInfo
            } );
        } );
        return deferred;
    }

    addWmsLayer(map).then(function (e) {

        function createLayer()
        {
            var layer, url;
            url = e.layer.url + "?_c=" + Math.random ();
            layer = new e.layer.constructor ( url, {
                format : e.layer.getImageFormat (),
                transparent : e.layer.imageTransparency,
                visibleLayers : array.map ( e.layer.visibleLayers, "return item;" ),
                resourceInfo : e.resourceInfo
            });
            return layer;
        }

        function refreshLayer(layer)
        {
            map.addLayer(layer);
            aspect.after(layer, "onUpdateEnd", function () {
                $.delay(5000, "UpdateWmsLayer", function () {
                    map.removeLayer(layer);
                    refreshLayer(createLayer());
                });
            });
        }

        refreshLayer ( createLayer () );

    });
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.