Skip to content

Instantly share code, notes, and snippets.

@JimBlaney
Created October 27, 2014 19:06
Show Gist options
  • Save JimBlaney/932194537d151f8c151b to your computer and use it in GitHub Desktop.
Save JimBlaney/932194537d151f8c151b to your computer and use it in GitHub Desktop.
Esri JSAPI layer to support STYLES argument on WMSLayer
define([
"dojo/_base/array",
"dojo/_base/declare",
"dojo/_base/lang",
"esri/layers/WMSLayer"
], function(
array,
declare,
lang,
WMSLayer
) {
/*
* for each WMSLayerInfo, a "style" property has been added to indicate the
* active style for the individual layer. update the layerInfo's style property
* and finish with a call to layer::refresh() to update the layer in the map.
*/
var StyledWMSLayer = declare(WMSLayer, {
getImageUrl: function(extent, width, height, callback) {
WMSLayer.prototype.apply(this, [
extent,
width,
height,
lang.hitch(this, function(url) {
url = url.replace("STYLES=&", lang.replace("STYLES={styles}&", {
styles: array.map(array.filter(this.layerInfos, function(li) {
return this.visibleLayers.indexOf(li.name) !== -1;
}, this), function(li) {
return li.style;
})
}));
callback(url);
})
]);
}
});
return StyledWMSLayer;
});
@stoffen
Copy link

stoffen commented Dec 14, 2015

Hi Jim, thanks for sharing. Could not get your code running , so I did a similar stunt based on Esri´s custom layer example:

dojo.declare("StyledWMSLayer", esri.layers.WMSLayer, {
    constructor: function() {
        this.onLoad(this);
    },

    getImageUrl: function(extent, width, height, callback) {
        var params = {
            request: "GetMap",
            format: this.imageFormat,
            layers: this.visibleLayers,
            styles: "styles",

            bbox:extent.xmin + "," + extent.ymin + "," + extent.xmax + "," + extent.ymax,
            srs: "EPSG:" + extent.spatialReference.wkid,
            width: width,
            height: height
        };

        callback(this.url + "&" + dojo.objectToQuery(params));
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment