Commit 26d91f45 authored by Sp3r4z's avatar Sp3r4z
Browse files

[Script] Add remove last created point

Closes #2
parent 7ff44b92
......@@ -5,19 +5,45 @@ getPostcodeByName = name => {
})
},
createMarker = (lat,lng) => {
addToLayerGroup( L.marker([lat, lng]) )
return L.marker([lat, lng])
},
createCircle = (lat,lng) => {
addToLayerGroup( L.circle([lat,lng], {radius: 100000}) )
return L.circle([lat,lng], {radius: 100000})
},
createPoint = (lat,lng,departement=null) => {
let marker = createMarker(lat,lng),
circle = createCircle(lat,lng),
layerArray = [marker,circle],
layer = null
if(departement != null) {
fetch('geojson/'+departement+'.geojson')
.then(response => response.json())
.then(datas => {
layerArray.push( L.geoJSON(datas) )
})
}
layer = L.layerGroup(layerArray)
layersArray.push(layer)
addToLayerGroup(layer)
},
addToLayerGroup = (elm) => {
layerGroup.addLayer(elm)
},
removeLastPoint = _ => {
if(layersArray.length > 0) {
let layer = layersArray.pop()
layerGroup.removeLayer(layer)
}
},
clearLayer = _ => {
layerGroup.clearLayers()
}
let theMap = L.map('map').setView([46.415,1.461], 6),
layersArray = [],
layerGroup = L.layerGroup()
layerGroup.addTo(theMap)
......@@ -30,13 +56,10 @@ L.Control.geocoder({
defaultMarkGeocode: false
})
.on('markgeocode', function(e) {
var center = e.geocode.center
createCircle(center.lat,center.lng)
createMarker(center.lat,center.lng)
let postCode = e.geocode.properties.address.postcode,
let center = e.geocode.center,
postCode = e.geocode.properties.address.postcode,
departementRef = null
if(postCode != null) {
departementRef = postCode.substring(0,2).toUpperCase()
}
......@@ -44,23 +67,15 @@ L.Control.geocoder({
departementRef = getPostcodeByName(e.geocode.properties.address.county)[0].code
}
if(departementRef != null) {
fetch('geojson/'+departementRef+'.geojson')
.then(response => response.json())
.then(datas => {
addToLayerGroup( L.geoJSON(datas) )
})
}
createPoint(center.lat,center.lng,departementRef)
})
.addTo(theMap)
theMap.on('click', function(e) {
createCircle(e.latlng.lat,e.latlng.lng)
createMarker(e.latlng.lat,e.latlng.lng)
createPoint(e.latlng.lat,e.latlng.lng)
})
const customControl = L.Control.extend({
const customControlClean = L.Control.extend({
options: {
position: 'topleft'
},
......@@ -75,6 +90,23 @@ const customControl = L.Control.extend({
return clearButton;
}
}),
customControlPoint = L.Control.extend({
options: {
position: 'topleft'
},
onAdd: function () {
let removeLastLayer = L.DomUtil.create('button')
removeLastLayer.innerHTML = "Défaire le dernier point"
removeLastLayer.onclick = ev => {
L.DomEvent.stopPropagation(ev);
removeLastPoint()
}
return removeLastLayer;
}
})
theMap.addControl(new customControl());
theMap.addControl(new customControlPoint());
theMap.addControl(new customControlClean());
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