I'm working with Leaflet
maps and I want to use a button to change the marker positions. The problem is that my map is initialized.
Uncaught Error: Map container is already initialized.
I've been reading some posts, but I can't do it correctly.
This one, suggests using this line before initialize the map but it's not working (probably I'm doing it wrong) document.getElementById('issMap').innerHTML = "<div id='map'></div>";
.
Some others solutions suggest using map.remove()
but it is not working as well.
Reading the leaflet's documentation I think I just have to remove a layer and put another one, but I don't know-how.
My code,
function mapa(coords){
centerMap_coords = centerMap(coords)
if (map != undefined) { map.removeLayer(); } //Not working
var map = L.map("issMap", {
center: [centerMap_coords[0], centerMap_coords[1]],
zoom: 13,
gestureHandling: true
});
const attribution = '©<a href="https://www.openstreetmap.org/copyright/">OpenStreetMap</a> contributors';
const tileUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
const tiles = L.tileLayer(tileUrl, {attribution}).addTo(map);
$('#btn_map').click(function(){
if( $('#ultima_posicion_check').is(':checked') ) {
var coords = [37.363,2.336];
mapa(coords);
}else if($('#ruta_ultimas_posiciones_check').is(':checked')){
var coords = [39.321,2.258];
mapa(coords);
} else{
alert('Checkbox empty');
}
Thank you!