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();
var done;
variable definition outsideloadGeoJson_cycliste
function to make it global variable, so value will persist from call to call.