map is not removed completely on `map.remove()`: `Uncaught TypeError: Cannot read property '_leaflet_pos' of undefined`
See original GitHub issueHow to reproduce
- Leaflet version I’m using: 1.2.0
- Browser (with version) I’m using: Chrome Version 60.0.3112.113
- It works fine in Firefox and Safari (havn’t tested in IE, Edge)
- OS/Platform (with version) I’m using: macOS Sierra
- add map in div element and add layer
this.leafletMap = new L.Map( <element> , {
zoomControl: true,
dragging: this.isInDragMode,
touchZoom: false,
scrollWheelZoom: false,
doubleClickZoom: false,
tap: false,
}
L.tileLayer( 'http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
} ).addTo( this.leafletMap );
- Remove the map on some user action
if (this.leafletMap ){
this.leafletMap.eachLayer(function(layer){
layer.remove();
});
this.leafletMap.remove();
this.leafletMap = null;
}
What behaviour I’m expecting and which behaviour I’m seeing
- Post removal of the map, it removes the map from the element, however, if I do double click on the div, it throws error -
Uncaught TypeError: Cannot read property '_leaflet_pos' of undefined
It seems like DOM element is still holding the event listeners even though the map and layers are removed.
Minimal example reproducing the issue
- this example is as simple as possible
- this example does not rely on any third party code
Using http://playground-leaflet.rhcloud.com/ or any other jsfiddle-like site.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:10
- Comments:39 (13 by maintainers)
Top Results From Across the Web
ERROR TypeError: Cannot read property '_leaflet_pos' of ...
I tried not calling setZoom at all, or calling setZoom before I remove the old markers. But then it throws appendChild of undefined...
Read more >typeerror: cannot read properties of undefined ...
Hello, im having issue with Cannot read properties of undefined (reading 'leafletpos'). Im using two components and two maps. One map enabled when...
Read more >Can Not Read Properties of Undefined Reading Map in React ...
If you are looking at can not read properties of undefined reading map in react js error or map function is not displaying...
Read more >Leaflet And Mapquest: Uncaught In Promise Typeerror
Uncaught TypeError: Cannot read property 'call' of undefined at e.fire leaflet.js:5 at e ... map is not removed completely on map.remove from the...
Read more >Leaflet.markercluster | Marker Clustering plugin for Leaflet
Adding and removing Markers; Bulk adding and removing Markers ... Clusters and markers too far from the viewport are removed from the map...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@spydmobile here goes, this is what i did in a slightly modified form: I have no idea how to post code properly in this f*ing comment field, sorry bout that. I have edited my own comment about 10 times now lol
Hi there – I think I’m experiencing this issue as well. Here’s my basic use-case:
I’m building a viewer component (using the Leaflet-IIIF plugin, but I don’t think that impacts anything here) for objects with multiple pages / surfaces as opposed to displaying an actual map. When the viewer loads, there is a series of thumbnails which the user can click to update which view of an object is displayed in the central area of the UI.
When the user changes the view, I’m calling
map.remove()
before setting up a new map for the new view. The new map is created on the same DOM element as the old one (a div with an ID), and I’m not modifying the DOM in any way outside of Leaflet.On the initial view everything works fine. But after calling
map.remove()
and showing a new view, the console complains:Cannot read property '_leaflet_pos' of undefined
whenever the map is dragged or zoomed.I can try to post a minimal example at some point, but this seems to be the same problem. This error comes up in Chrome but not in Firefox.