Commit d3968798 authored by Gérald Niel's avatar Gérald Niel
Browse files

Cache des geojson département en mémoire

parent 4fb76f15
......@@ -9,7 +9,7 @@
*/
window.addEventListener('load', async () => {
const mapBounds = L.latLngBounds( L.latLng(51.0884, 9.5597), L.latLng(41.367, -5.103600) ),
const mapBounds = L.latLngBounds( L.latLng(51.0884, 9.5597), L.latLng(41.367, -5.103600) ),
metropole = await getJsonAsync('geojson/metropole-et-outre-mer.geojson'),
departements = await getJsonAsync('assets/departements.json'),
ign_attribution = '<a href="https://geoservices.ign.fr/">IGN-F/Geoportail</a>',
......@@ -164,7 +164,8 @@ window.addEventListener('load', async () => {
let layersArray = [],
layerGroup = new L.layerGroup(),
locate = -1; // flag pour la position
locate = -1, // flag pour la position
cacheDpt = {}; // Cache geojson département en mémoire
layerGroup.addTo(theMap);
......@@ -316,7 +317,7 @@ window.addEventListener('load', async () => {
L.DomEvent.stopPropagation( e );
L.DomEvent.preventDefault( e );
// Ne pas recréer le layer si déjà existant et zoomer sur la position
if ( locate === -1 )
if ( locate === -1 )
navigator.geolocation.getCurrentPosition( drawPosition );
else
fitBounds( [ layersArray[locate] ] );
......@@ -393,10 +394,14 @@ window.addEventListener('load', async () => {
circle = createCircle(lat, lng),
layers = [ marker, circle ],
layer = null,
datas = null;
geojson = null;
if ( departement ) datas = await getJsonAsync('geojson/' + departement + '.geojson');
if ( datas ) layers.push( new L.geoJSON(datas) );
if ( departement ) {
if (!cacheDpt[departement])
cacheDpt[departement] = new L.geoJSON( await getJsonAsync('geojson/' + departement + '.geojson') );
geojson = cacheDpt[departement];
}
if ( geojson ) layers.push( geojson );
layer = new L.FeatureGroup( layers );
layersArray.push( layer );
addToLayerGroup( layer );
......@@ -473,8 +478,8 @@ window.addEventListener('load', async () => {
function fitBounds( l ) {
let i = l ? l.length - 1 : -1,
b = i > -1 ? l[i].getBounds() : mapBounds;
theMap.fitBounds(b, {
padding: [10, 10]
theMap.fitBounds(b, {
padding: [10, 10]
});
}
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment