Leaflet: Error: Map Container Is Already Initialized

Created on 24 Oct 2015  路  20Comments  路  Source: Leaflet/Leaflet

refreshing a leaflet map giving Error: Map Container Is Already Initialized

Most helpful comment

Before initializing map check for is the map is already initiated or not

var container = L.DomUtil.get('map');
if(container != null){
container._leaflet_id = null;
}

All 20 comments

Sounds like you are trying to re-init an existing map... The issues boards here is more for working through bugs and suggestions of new features. StackOverflow(http://stackoverflow.com/questions/tagged/leaflet) is a better place for troubleshooting your usage of the library. If you do believe this is from a bug, post your example case in a JSFiddle or another demo site.

Looks like issue is related to implementation in MVVM and not a bug. Will try to search some MVVM examples using leaflet.

same issue here... what could it be?

@ralexrdz perhaps a container that you initialize more than once?

@mourner The html is basically html head loading js and css and a div with id="map". And the only js that I have is
map.setView([19.34, -99.15], 12);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
attribution: 'Map data © OpenStreetMap contributors, CC-BY-SA, Imagery 漏 Mapbox',
maxZoom: 18,
id: 'mapboxMapId,
accessToken: 'mapboxAccessToken'
}).addTo(map);
How could that be that a container is initialize before?

i am having the same issue, i am using leaflet in React

That'll be a react-leaflet bug probably @epicallan

@danzel i am using react, its react-leaflet bug, just did some hacky work arounds

@epicallan Can you please explain what you did? I have the same issue with React and Leaflet.

Before initializing map check for is the map is already initiated or not

var container = L.DomUtil.get('map');
if(container != null){
container._leaflet_id = null;
}

@Dipinrajc With this method your not available to drag with mouse on the map.

Any ideia how to drag map again with mouse @adrielstar ?

I us this @fillipefeitosa .

HTML:
< div id="map" >< /div>

render map:
document.getElementById('map').innerHTML = "< div id='map' style='width: 100%; height: 100%;'>

";

to reload content of div where render map.
Hoop this will help you too

Thanks friend, but that did not work. It replaced my var map with the div and map not loads

Before initializing map check for is the map is already initiated or not

var container = L.DomUtil.get('map');
if(container != null){
container._leaflet_id = null;
}

is there an existent leaflet method for this?

same problem with me :(

Before initializing map check for is the map is already initiated or not
var container = L.DomUtil.get('map');
if(container != null){
container._leaflet_id = null;
}

is there an existent leaflet method for this?

@enzojimenez , You can create one method for this as I have created below

function initializingMap() // call this method before you initialize your map.
{
var container = L.DomUtil.get('map');
if(container != null){
container._leaflet_id = null;
}
}

Para resolver el problema, define:
import * as Map from 'leaflet';

@ViewChild('map') mapElement: ElementRef;
map: Map.Map;

this.map = Map.map(this.mapElement.nativeElement, {
maxZoom: 18
});

Solucion aqu铆: https://www.designlimbo.com/leaflet-ionic-3-and-map-container-is-already-initialized/

as said here, this would be the best way of doing it:
if (map != undefined) { map.remove(); }

Before initializing map check for is the map is already initiated or not

var container = L.DomUtil.get('map');
if(container != null){
container._leaflet_id = null;
}

You are an Angel

Was this page helpful?
0 / 5 - 0 ratings

Related issues

map is not removed completely on map.remove() from the DOM
jpatel3 picture jpatel3  路  37Comments

Latest Chrome introduced 1px tile borders to integer zooms
hyperknot picture hyperknot  路  30Comments

Contextmenu is not triggered on iOS13
ginger-777 picture ginger-777  路  47Comments

Scrollwheel Zoom issue in Chrome
MuellerMatthew picture MuellerMatthew  路  70Comments

Map tiles not loading until a zoom is executed
rwwagner90 picture rwwagner90  路  61Comments