I am trying to load a map by using leaflet. when i refresh the map, I get the above error. I studied other proposed answers for this question. But, non of them worked for me. I am trying to load a map inside a function which is run by a onclick event. Here is the code:
function load_map_and_analyze_data(){
var mymap = L.map('mapid',{ center: new L.LatLng(the_center_splitted[0],the_center_splitted[1]),maxZoom: 17, minZoom:11, zoom: 14}); //creating the map
//the rest of analyze and code goes here
}
The tested proposed answers:
1- Check if my map is initialized, if yes remove it, and define it once again.
console.log(mymap);
if(mymap != 'undefined' || mymap != null) {
mymap.remove();
}
Result: mymap is undefined whenever i refresh the function and just the same error.
2- Defining this variable as a general variable outside of function just when mapdiv dom is ready. then i used jquery.
$( "#mapid" ).load(function() {
var mymap= L.map('mapid');
});
Result: this error: Map container not found.
3- Removing the mydiv dom and just trying to recreate it inside of the function.
console.log(mymap);
if(mymap != undefined || mymap != null){
mymap.remove();
$("#mapdiv").html("");
$( "<div id=\"mapdiv\" style=\"height: 500px;\"></div>" ).appendTo(document);
}
Result: mymap is undefined and just the code has not run to test its efficiency.
Any idea or suggestion is appreciated. Thank you.