Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the map object for a Leaflet map from the ID of the div element?

A previous developer created a map using Leaflet on the page. The Javascript is very complex and I cannot find where the Leaflet map is instantiated. However, I know which DOM element is associated with the map. Is there any way to get the Leaflet map object from the id of the element?

I.e., the element id is #city-map. L.map('city-map') returns

Error: Map container is already initialized.
like image 314
jdelman Avatar asked Sep 02 '14 16:09

jdelman


People also ask

How do you make a leaflet marker?

Adding a Simple Marker Step 1 − Create a Map object by passing a <div> element (String or object) and map options (optional). Step 2 − Create a Layer object by passing the URL of the desired tile. Step 3 − Add the layer object to the map using the addLayer() method of the Map class.


1 Answers

No: Leaflet does not set a reference back to the map object from the DOM element, because that can easily cause memory leaks. And L.map always creates a new map - it does not find a map like you might expect coming from jQuery. You'll need to decipher the code from your other developer and find the variable the map is assigned to.

like image 148
tmcw Avatar answered Sep 17 '22 19:09

tmcw