lundi 9 novembre 2020

Loop list WMS Leaflet Jquery

I am taking from a postgres table a list of 3 wms layers and I want to return in the map. I use the Leaflet.wms.js library. The error i am getting is pure JS JQuery error. This is not GIS related problem.

The code doesn't give me any layer in the map. Where is the problem? Here I insert the code with some examples

<div id="layer_test_ptpr_id"></div>

//start sidebar.js
$("#selectall_aree_protette").click(function () {
    // Remove unchecked layers
    $('input:checkbox[name="aree_protette-layers"]').prop("checked", $("#selectall_aree_protette").prop("checked")).each(function () {
        map.removeLayer(window[$(this).attr('id')]);
        if ($('#' + $(this).attr('id')).is(':checked')) {
            map.addLayer(window[$(this).attr('id')]);
        }
//sidebar.js:351 Errors in the browser debugger console:
//if ($('#' + $(this).attr('id')).is(':checked')) //map.addLayer(window[$(this).attr('id')]);}

    });
});
$('input:checkbox[name="aree_protette-layers"]').change(function () {
    var layers = [];
    function sortByKey(array, key) {
        return array.sort(function (a, b) {
            var x = a[key];
            var y = b[key];
            return ((x < y) ? -1 : ((x > y) ? 1 : 0));
        });
    }
    if ($('#' + $(this).attr('id')).is(':checked')) {
        $('input:checkbox[name="aree_protette-layers"]').each(function () {
            // Remove all overlay layers
            map.removeLayer(window[$(this).attr('id')]);
            if ($('#' + $(this).attr('id')).is(':checked')) {
                // Add checked layers to array for sorting
                layers.push({
                    'z-index': $(this).attr('z-index'),
                    'layer': $(this)
                });
            }
        });
        // Sort layers array by z-index
        var orderedLayers = sortByKey(layers, 'z-index');
        // Loop through ordered layers array and add to map in correct order
        $.each(orderedLayers, function () {
            map.addLayer(window[$(this)[0].layer[0].id]);
        });
    } else {
        // Simply remove unchecked layers
        map.removeLayer(window[$(this).attr('id')]);
    }
});
//end sidebar.js

$.getJSON("http://179.16.79.218:8080/geoserver/ows?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=permitting:pgis_test_loop_ptpr&SRSNAME=EPSG:4326&outputFormat=application%2Fjson", function(json) {
    var layer_test_ptpr = [];
    var layerList = new Array();

     var i;

     for ( i=0; i<json.features.length; i++) {
     
         if (json.features[i].properties.tab === "ptpr") {

     
             var url_GetFeatureInfo = json.features[i].properties.url_layer + "SERVICE=WMS&VERSION=1.3.0&REQUEST=GetFeatureInfo";
             var id_layer = json.features[i].properties.id_layer;
             var url_QUERY_LAYERS = "&QUERY_LAYERS=" + id_layer;
             var layers = "&LAYERS=" + id_layer; 
             var source_vincoli = L.WMS.source(url_GetFeatureInfo + url_QUERY_LAYERS  + layers, options);
             layerList[i] = source_vincoli.getLayer(id_layer); 
             

             console.log(layerList[i])
             
             var prodResultContainer = 
             '<div class="checkbox">' 
                +'<label class="container">'
                 + '<input type="checkbox" name="aree_protette-layers"' 
                 + 'id="'
                 + layerList[i]
                + '"z-index="4">'
                 + '<span class="checkmark" rel="tooltip" data-toggle="tooltip" data-placement="bottom" title="ON/OFF Layer"></span>'
                 + '<a class="container_layer" href="#" id="meta_layer_SitiprotettiVIElencoufficialeareeprotetteEUAP_56" rel="tooltip" data-toggle="tooltip" data - placement="bottom" title = "Apri Metadato">'
                 + json.features[i].properties.alias
                 +'</a></label></div>';


             

        layer_test_ptpr.push(prodResultContainer);
        
             
       }
        }
       
    $('#layer_test_ptpr_id').html(layer_test_ptpr);

    console.log(layer_test_ptpr);

});
//errors in the browser debugger console
Uncaught Error: Syntax error, unrecognized expression: #[object Object]
    at Function.ga.error (jquery-2.1.4.min.js:2)
    at ga.tokenize (jquery-2.1.4.min.js:2)
    at ga.select (jquery-2.1.4.min.js:2)
    at Function.ga [as find] (jquery-2.1.4.min.js:2)
    at n.fn.init.find (jquery-2.1.4.min.js:2)
    at new n.fn.init (jquery-2.1.4.min.js:2)
    at n (jquery-2.1.4.min.js:2)
    at HTMLInputElement.<anonymous> (sidebar.js:351)

//sidebar.js:351 Errors in the browser debugger console:
//if ($('#' + $(this).attr('id')).is(':checked')) //map.addLayer(window[$(this).attr('id')]);}

    at Function.each (jquery-2.1.4.min.js:2)
    at n.fn.init.each (jquery-2.1.4.min.js:2)



Aucun commentaire:

Enregistrer un commentaire