1

I have a WFS layer, updated in a loop, and I want the view to be centered on it only the first time the map is loaded; next, when the user moves the map or waits 20 secs, there is a movend function that reloads the wfs, but I don't want the view to be recentered again. So I tried a variable, but I'm not successful

    function loadGeoJson_cycliste(data) {
            var done;
    if (done === 'OK')
            { featureLayer_cycliste.clearLayers();
        featureLayer_cycliste.addData(data); }
            else {
    featureLayer_cycliste.clearLayers();
    featureLayer_cycliste.addData(data);
                    map.fitBounds(featureLayer_cycliste.getBounds)
    });
             var done = 'OK';
       


     };
    
function load_wfs_cycliste() {
    if (map.getZoom() > startatzoom_cycliste) {
        var geoJsonUrl = 'http://localhost:8080/geoserver/wfs?';
        var defaultParameters = {
            service: 'WFS',
            version: '1.0.0',
            request: 'getFeature',
            typeName: wfs_cycliste,
            maxFeatures: 100,
            outputFormat: 'text/javascript',
            format_options: 'callback: getJson_cycliste',
            srsName: 'EPSG:4326'
        };

          var customParams = {
            bbox: map.getBounds().toBBoxString()
        };

        var parameters = L.Util.extend(defaultParameters, customParams);
 //       console.log(geoJsonUrl + L.Util.getParamString(parameters));

        $.ajax({
            jsonp: false,
            url: geoJsonUrl + L.Util.getParamString(parameters),
            dataType: 'jsonp',
            jsonpCallback: 'getJson_cycliste',
            success: loadGeoJson_cycliste
        });
      
          

    } else {
        featureLayer_cycliste.clearLayers();
    }
};

trigger :

function reloadcom() {
if (map.hasLayer(featureLayer_cycliste)) load_wfs_cycliste();

                setTimeout(reloadcom, 20000);
        };

 
featureLayer_cycliste.on('add', function(evt) {
        load_wfs_cycliste();
});

map.on('moveend', reloadcom);

reloadcom();
3
  • @TomazicM I edited with map. fitbounds, featurelayer get bounds, and it was zooming to the extent of the layer but coulnt get out or zoom out
    – Vincent Dc
    Jun 29, 2022 at 17:40
  • Just move your var done; variable definition outside loadGeoJson_cycliste function to make it global variable, so value will persist from call to call.
    – TomazicM
    Jun 29, 2022 at 18:28
  • @Tomazic Thanks. I solved it through duplicating the functions. I'm sorry I didn't include the load_wfs_cycliste function
    – Vincent Dc
    Jul 7, 2022 at 9:48

1 Answer 1

0

I solved it by duplicating the functions. onAdd now call nested "firstload" functions which call the bounds; they would do that only once.

2nd and next loads :

    function loadGeoJson_cycliste(data) {
     featureLayer_cycliste.clearLayers()
        featureLayer_cycliste.addData(data)
};    

function load_wfs_cycliste() {
    if (map.getZoom() > startatzoom_cycliste) {
        var geoJsonUrl = 'http://localhost:8080/geoserver/wfs?';
        var defaultParameters = {
            service: 'WFS',
            version: '1.0.0',
            request: 'getFeature',
            typeName: wfs_cycliste,
            maxFeatures: 100,
            outputFormat: 'text/javascript',
            format_options: 'callback: getJson_cycliste',
            srsName: 'EPSG:4326'
        };

          var customParams = {
            bbox: map.getBounds().toBBoxString()
        };

        var parameters = L.Util.extend(defaultParameters, customParams);
 //       console.log(geoJsonUrl + L.Util.getParamString(parameters));

        $.ajax({
            jsonp: false,
            url: geoJsonUrl + L.Util.getParamString(parameters),
            dataType: 'jsonp',
            jsonpCallback: 'getJson_cycliste',
            success: loadGeoJson_cycliste
        });
      
          

    } else {
        featureLayer_cycliste.clearLayers();
    }
};

first load :

var tdfbounds;
var mCenterlat;
var mCenterlng;

function loadGeoJson_cycliste_firstload(data) {

         featureLayer_cycliste.clearLayers()
         featureLayer_cycliste.addData(data)

    var tdfbounds = featureLayer_cycliste.getBounds();
        var mCenterlat  = tdfbounds.getCenter().lat;
        var mCenterlng  = tdfbounds.getCenter().lng;
        map.setView(new L.LatLng(mCenterlat, mCenterlng), 10);
};


function load_wfs_cycliste_firstload() {
    if (map.getZoom() > startatzoom_cycliste) {
        var geoJsonUrl = 'http://localhost:8080/geoserver/wfs?';
        var defaultParameters = {
            service: 'WFS',
            version: '1.0.0',
            request: 'getFeature',
            typeName: wfs_cycliste,
            maxFeatures: 100,
            outputFormat: 'text/javascript',
            format_options: 'callback: getJson_cycliste',
            srsName: 'EPSG:4326'
        };

          var customParams = {
            bbox: map.getBounds().toBBoxString()
        };

        var parameters = L.Util.extend(defaultParameters, customParams);
 //       console.log(geoJsonUrl + L.Util.getParamString(parameters));

        $.ajax({
            jsonp: false,
            url: geoJsonUrl + L.Util.getParamString(parameters),
            dataType: 'jsonp',
            jsonpCallback: 'getJson_cycliste',
            success: loadGeoJson_cycliste_firstload
        });



    } else {
        featureLayer_cycliste.clearLayers();
    }
};

triggers :

function reloadcom() {
if (map.hasLayer(featureLayer_cycliste)) load_wfs_cycliste();

                setTimeout(reloadcom, 20000);
        };

 
featureLayer_cycliste.on('add', function(evt) {
        load_wfs_cycliste_firstload();
});

map.on('moveend', reloadcom);

reloadcom();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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