Commit 4ce67ba7 authored by Sp3r4z's avatar Sp3r4z
Browse files

[Script] Add multiple points

+ refactoring createCircle function
parent 3de00831
......@@ -5,19 +5,28 @@ getPostcodeByName = name => {
})
},
createMarker = (lat,lng) => {
if(marker != null) marker.removeFrom(theMap)
marker = L.marker([lat, lng])
marker.addTo(theMap)
layerGroupArray.push( L.marker([lat, lng]) )
},
createCircle = (lat,lng) => {
layerGroupArray.push( L.circle([lat,lng], {radius: 100000}) )
},
showLayer = _ => {
layerGroup = L.layerGroup(layerGroupArray)
layerGroup.addTo(theMap)
},
clearLayer = _ => {
theMap.removeLayer(layerGroup)
layerGroupArray = []
layerGroup = null
}
let theMap = L.map('map').setView([46.415,1.461], 6),
circle = null,
departement = null,
marker = null
layerGroupArray = [],
layerGroup = null
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>',
maxZoom: 19,
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>',
maxZoom: 19,
}).addTo(theMap)
L.Control.geocoder({
......@@ -25,11 +34,8 @@ L.Control.geocoder({
})
.on('markgeocode', function(e) {
var center = e.geocode.center
if(departement != null) departement.removeFrom(theMap)
if(circle != null) circle.removeFrom(theMap)
circle = L.circle([center.lat, center.lng], {radius: 100000})
circle.addTo(theMap);
createCircle(center.lat,center.lng)
createMarker(center.lat,center.lng)
let postCode = e.geocode.properties.address.postcode,
......@@ -45,8 +51,8 @@ L.Control.geocoder({
fetch('geojson/'+departementRef+'.geojson')
.then(response => response.json())
.then(datas => {
departement = L.geoJSON(datas)
departement.addTo(theMap)
layerGroupArray.push( L.geoJSON(datas) )
showLayer()
})
}
......@@ -54,9 +60,27 @@ L.Control.geocoder({
.addTo(theMap)
theMap.on('click', function(e) {
if(departement != null) departement.removeFrom(theMap)
if(circle != null) circle.removeFrom(theMap)
circle = L.circle([e.latlng.lat, e.latlng.lng], {radius: 100000})
createCircle(e.latlng.lat,e.latlng.lng)
createMarker(e.latlng.lat,e.latlng.lng)
circle.addTo(theMap);
});
showLayer()
})
const customControl = L.Control.extend({
options: {
position: 'topleft'
},
onAdd: function () {
let clearButton = L.DomUtil.create('button')
clearButton.innerHTML = "Réinitialiser la carte"
clearButton.onclick = ev => {
L.DomEvent.stopPropagation(ev);
clearLayer()
}
return clearButton;
}
})
theMap.addControl(new customControl());
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