Add a WFS service in a Leaflet map and need to control the current scale

1.5k views Asked by At

I need to load a WFS service (points) in my Leaflet map.

I know how to load a WFS service in my map but I've to check dinamically the map current scale / extent because I've to limit the number of features I've to request to the server and to render on my map.

My service has a lot of points and I'd like to limit the visualization of my layer only when we are al level=18 of my basemap (OpenStreetMap).

Is there any way to check dinamically the map current extent / scale and so decide if invoke or not my WFS Service?

Any example?

Thank you very much in advance, any suggestion is appreciate!!!!

Cesare

1

There are 1 answers

0
Cesare On

I solved in this manner

        .....
        .....
        .....
            function onEachFeature(feature, layer) {
               if (feature.properties && feature.properties.popupContent) {
                       layer.bindPopup(feature.properties.popupContent);
               }
            };

            myGeoJSONLayeraddressPCN3 = L.geoJson(myGeoJSON, {
                            pointToLayer: function (feature, latlng) {
                                              return L.circleMarker(latlng, geojsonMarkerOptions);
                            },
                            onEachFeature: onEachFeature
            });

            var baseLayers = {
                    "OSM": background
            };

            var overlays = {
                   "My GeoJSON Layer name": myGeoJSONLayer
            };

            map = new L.Map('map', {
                                    center: [42, 12],
                                    zoom: 6,
                                    layers: [background]

            });

            // *** add base layers and overlay layers list to map ... ***
            TOC = L.control.layers(baseLayers, overlays);
            TOC.addTo(map);                              

            map.on('overlayadd', function(e) {
                                    map.removeLayer(myGeoJSONLayer); 
                                    TOC.removeLayer(myGeoJSONLayer);
                                    if ((e.name == "My GeoJSON Layer name") && (map.getZoom() < 18)){
                                     alert('To view the layer zoom at max details ...');
                                     TOC.addOverlay(myGeoJSONLayer, "My GeoJSON Layer name");
                                     isOnMap = 0;
                                    } 
                                    if ((e.name == "My GeoJSON Layer name") && (map.getZoom() >= 18)){
                                     var currentExtent = map.getBounds().toBBoxString();
                                     invokeService(currentExtent);
                                    } 
                                 });

            map.on('overlayremove', function(e) {
                                    if (e.name == "My GeoJSON Layer name"){
                                     isOnMap = 0;
                                    } 
                                 });

            map.on('dragend', function(e) {
                                    if ((map.getZoom() >= 18) && (isOnMap == 1)){
                                     map.removeLayer(myGeoJSONLayer); 
                                     TOC.removeLayer(myGeoJSONLayer);
                                     var currentExtent = map.getBounds().toBBoxString();
                                     invokeService(currentExtent);
                                    } 
                                 });

            map.on('zoomend', function(e) {
                                    if (map.getZoom() < 18) {
                                     map.removeLayer(myGeoJSONLayer); 
                                     TOC.removeLayer(myGeoJSONLayer);
                                     TOC.addOverlay(myGeoJSONLayer, "My GeoJSON Layer name");
                                    } 
                                 });

and I've also a function invokeService() where I've ...

function invokeService (extent) {
             .....
             .....
             .....
                    function onEachFeature(feature, layer) {
                      if (feature.properties && feature.properties.popupContent) {
                         layer.bindPopup(feature.properties.popupContent);
                      }
                     }

                     myGeoJSONLayeraddressPCN3 = L.geoJson(MyGeoJSON, {
                                     pointToLayer: function (feature, latlng) {
                                                   return L.circleMarker(latlng, geojsonMarkerOptions);
                                     },
                                     onEachFeature: onEachFeature
                     });

                     addressPCN3.addTo(map);                         
                     TOC.addOverlay(addressPCN3, "My GeoJSON Layer name");
                     isOnMap = 1;
               }
             .....
             .....
             .....
}

It's working now!