4

I have a style pulled from my GeoServer and have created a basic legend, with a fixed PNG. However I was wondering if it is possible to have this legend be pulled from the layers SLD? so that if the SLD is changed the legend will update too?

This is the Legend code

    var legend = L.control({position: 'bottomright'});

    legend.onAdd = function (map) {
    var div = L.DomUtil.create('div', 'info legend');

        div.innerHTML +=
        '<img src="SymbolDelete.png" alt="legend" width="134" height="147">';

    return div;
    };

    legend.addTo(map);
5

Instead of targeting a static PNG file, you could target the output of GeoServer's GetLegendGraphic service, which can return the legend for a given layer as a PNG file e.g.

legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend');

    div.innerHTML +=
    '<img src="http://localhost:8080/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=20&LAYER=topp:states" alt="legend" width="134" height="147">';

return div;
};
  • Changed topp:states to my layer and worked first time – Tom Sep 25 '13 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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