jqueryleafletgeoserverwmssld

jQuery, Leaflet : how to refresh wms layer on change event?


I use jQuery, GeoServer and Leaflet.

I have a json containing names of SLD. With a dropdown, I put the name of the SLD in the parameters of the layer. When I select a style, the map doesn’t refresh but the style is assigned to the layer.

So it works except that I don’t have a dynamic refresh. You have to zoom to see the change.

Do you have an idea to work around the problem? I have the impression that Leaflet doesn't have an update() function.

I have tried some workarounds as with the invalidateSize() function but it doesn’t work. Maybe my methodology is not good. What do you think ?

Thks !

below, sld.json (with id and nom=name of SLD stored in Geoserver (if you know a more dynamic method to recover the SLD I'm interested !))

[{"id" : "1", "nom" : "demo_secteur_statut"}, {"id" : "2", "nom" : "demo_secteur_statut_"}, {"id" : "3", "nom" : "demo_secteur_1"}]

JS

function Zone() {
    $.ajax({
        type: "GET",
        url: "sld.json",
        dataType: "json",
        success: function(data) {
            $('#select-sld').empty();
            $('#select-sld').append("<option value='0'>-- Choisir une analyse --</option>");
            $.each(data, function(i, item) {
                $('#select-sld').append('<option value=' + data[i].id + '>' + data[i].nom + '</option>');
            });

            $("#select-sld").change(function(){
                var selectId = $("#select-sld option:selected").val();
                var getSLD = [];

                for (var i in data){     
                    if(data[i].id == selectId){  
                        getSLD += data[i].nom;
                        if(data[i].id != selectId){
                            return false;
                        }
                    }
                };
                //map2.removeLayer(secteur);
                //secteur.addTo(map2);
                //map2.invalidateSize();
                secteur.wmsParams.styles = getSLD;
            }); 
        },
                          complete: function() {}
    });

    var map2 = L.map('map2', { zoomControl:false, attributionControl:false }).setView([48.11, -1.67], 14);
    L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png').addTo(map2);

    var secteur = L.tileLayer.wms("http://localhost:8080/geoserver/cite/wms", {
        layers: 'cite: secteur',
        format: 'image/png',
        transparent: true
    });
    secteur.addTo(map2);
}

$(document).ready(function() {
    Zone();
});

HTML

<select id="zone-select"></select>

<div id="container-map">
       <div id="map2"></div>   
</div>

Solution

  • Also you can try this.

      var wmsLayerKey = Object.keys(map._layers)[1]; //whatever, your wmslayerkey
      var randomValue = Math.floor(Math.random() * 100000001); 
      map._layers[wmsLayerKey].setParams({random : randomValue});//dummy property: random