I'm trying to refresh the Leaflet map! I read the documentation and look on the internet but I can't find the solution ...
I tried a lot of method like map.remove () or map.viewreset or other solution read here or on the documentation.
I would obviously like the map to change when the user does a new search (by entering a new number and clicking on the button) and that the map is no longer visible if the number does not enter returns a status 200 (so either it is not a 14-digit sequence or the sequence does not exist in the API)
Thanks for your help :)
index.php :
<body>
<form id="form">
<label for="sic" class="sic"> Numéro de SIRET : </label>
<input type="text" id="sic" name="texte"/>
<input type="submit" value="envoyer" id="valider" class="send"/>
</form>
<p id="result"></p>
<div id="mapid"></div>
<script src="script.js"></script>
</body>
script.js
const form = document.getElementById('form');
form.addEventListener('submit',(ev) => {
ev.preventDefault();
let saisi = document.getElementById('sic').value;
if(isNaN(saisi) || saisi.length != 14){
document.getElementById('result').textContent = "Veuillez rentrer un numéro SIRET valable (14 chiffres)";
document.getElementById('sic').value = ' ';
}else{
fonctionTest(saisi);
}
})
function fonctionTest(siret){
let request = new XMLHttpRequest();
request.open("GET", 'apireq.php?texte='+siret, true);
request.addEventListener("readystatechange", () => {
if (request.readyState === XMLHttpRequest.DONE && request.status === 200) {
const reponse = JSON.parse(request.responseText);
addCoor(longitude,latitude)
}
});
function addCoor(long, lat){
const map = L.map('mapid').setView([lat, long], 14);
// map.viewreset;
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
}