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

Zoom sur le cercle & ne pas relocaliser la position

parent 30528299
......@@ -9,9 +9,9 @@
*/
window.addEventListener('load', async () => {
const metropole = await getJsonAsync('geojson/metropole-et-outre-mer.geojson'),
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'),
france = new L.geoJSON( await getJsonAsync('geojson/metropole.geojson') ),
ign_attribution = '<a href="https://geoservices.ign.fr/">IGN-F/Geoportail</a>',
ign_options = {
minZoom: 0,
......@@ -150,7 +150,7 @@ window.addEventListener('load', async () => {
}),
theMap = L.map('map');
initView();
fitBounds();
theMap.on('click', (e) => {
getReverse(e.latlng.lat, e.latlng.lng);
......@@ -163,7 +163,8 @@ window.addEventListener('load', async () => {
);
let layersArray = [],
layerGroup = new L.layerGroup();
layerGroup = new L.layerGroup(),
locate = -1; // flag pour la position
layerGroup.addTo(theMap);
......@@ -260,7 +261,7 @@ window.addEventListener('load', async () => {
L.DomEvent.stopPropagation( e );
L.DomEvent.preventDefault( e );
clearLayer();
initView();
fitBounds();
}
return container;
},
......@@ -287,7 +288,6 @@ window.addEventListener('load', async () => {
L.DomEvent.stopPropagation( e );
L.DomEvent.preventDefault( e );
removeLastPoint();
//initView();
}
return container;
},
......@@ -315,7 +315,11 @@ window.addEventListener('load', async () => {
.on(link, 'click', ( e ) => {
L.DomEvent.stopPropagation( e );
L.DomEvent.preventDefault( e );
navigator.geolocation.getCurrentPosition( drawPosition );
// Ne pas recréer le layer si déjà existant et zoomer sur la position
if ( locate === -1 )
navigator.geolocation.getCurrentPosition( drawPosition );
else
fitBounds( [ layersArray[locate] ] );
})
.on(link, 'dblclick', L.DomEvent.stopPropagation);
......@@ -356,7 +360,7 @@ window.addEventListener('load', async () => {
function drawPosition( p ) {
let lat = p.coords.latitude,
lng = p.coords.longitude;
getReverse( lat, lng );
getReverse( lat, lng, true );
}
function createCircle( lat, lng ) {
......@@ -397,6 +401,7 @@ window.addEventListener('load', async () => {
layersArray.push( layer );
addToLayerGroup( layer );
fitBounds( layersArray );
return layersArray.length - 1;
}
function clearLayer() {
......@@ -409,13 +414,15 @@ window.addEventListener('load', async () => {
function removeLastPoint() {
if (layersArray.length > 0) {
let layer = layersArray.pop();
layerGroup.removeLayer(layer);
let i = layersArray.length - 1;
layer = layersArray.pop();
layerGroup.removeLayer( layer );
fitBounds( layersArray );
if( i === locate ) locate = -1;
}
}
function getReverse( lat, lng ) {
function getReverse( lat, lng, p = false ) {
geoCoder.reverse({ 'lat': lat, 'lng': lng }, theMap.options.crs.scale(20), ( r ) => {
r = r[0] || null;
let address = r && r.properties ? r.properties.address : null,
......@@ -424,7 +431,9 @@ window.addEventListener('load', async () => {
if ( address && address.country == 'France') {
dpt = getDepartementRef( address );
createPoint(lat, lng, html, dpt);
createPoint(lat, lng, html, dpt).then( i => {
if (p) locate = i;
});
}
});
}
......@@ -460,12 +469,11 @@ window.addEventListener('load', async () => {
}
function fitBounds( l ) {
l = l && l[l.length - 1] ? l[l.length - 1] : france;
theMap.fitBounds( l.getBounds(), { padding: [10, 10] });
}
function initView() {
fitBounds();
let i = l ? l.length - 1 : -1,
b = i > -1 ? l[i].getBounds() : mapBounds;
theMap.fitBounds(b, {
padding: [10, 10]
});
}
});
// @license-end
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