Crash when fitBounds() to a zero-area bounds on uninitialized map
See original GitHub issueHow to reproduce
- Leaflet version I’m using: 1.0.2
- Browser (with version) I’m using: Chrome
- OS/Platform (with version) I’m using: OS
What behaviour I’m expecting and which behaviour I’m seeing
I discovered a bug when running the method fitBounds and use same bounds. After running i get the message: Uncaught Error: Invalid LatLng object: (NaN, NaN).
The problem occur in method getBoundsZoom in var scale = Math.min(size.x / boundsSize.x, size.y / boundsSize.y);
. This return Infinity and cause the error
Minimal example reproducing the issue
var map = L.map('map', {zoom: 15});
map.fitBounds([[42.897968, -83.25632], [42.897968, -83.25632]]);
Stack trace looks like:
Error: Invalid LatLng object: (NaN, NaN) leaflet-src.js:1609:9
L.LatLng https://unpkg.com/leaflet@1.0.0/dist/leaflet-src.js:1609:9
L.Projection.SphericalMercator.unproject https://unpkg.com/leaflet@1.0.0/dist/leaflet-src.js:2019:10
L.CRS.pointToLatLng https://unpkg.com/leaflet@1.0.0/dist/leaflet-src.js:2062:10
L.Map<.unproject https://unpkg.com/leaflet@1.0.0/dist/leaflet-src.js:2877:10
L.Map<._getBoundsCenterZoom https://unpkg.com/leaflet@1.0.0/dist/leaflet-src.js:2451:16
L.Map<.fitBounds https://unpkg.com/leaflet@1.0.0/dist/leaflet-src.js:2470:16
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
map.fitBounds(bounds) causing browser to freeze
I have a really strange error that keeps causing my browser to just freeze when I try to resize a map to fit...
Read more >Maps JavaScript API | Google Developers
Sets the viewport to contain the given bounds. Note: When the map is set to display: none , the fitBounds function reads the...
Read more >Fit a map to a bounding box | Mapbox GL JS
Fit the map to a specific area, regardless of the pixel size of the map. ... fitBounds() pans and zooms the map viewport...
Read more >Set view does not work after Fitbounds - GIS Stack Exchange
Instead of var foo = new L.featureGroup() , use var foo ... When fitting to bounds, the map will hit maxZoom , and...
Read more >Map fitBounds not applying padding when only one point ...
When using map.fitBounds(bounds, padding) it works great if my bounds have multiple points like: var bounds = new google.maps.LatLngBounds();
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
Really, this fixed the problem after set maxZoom=15. Thanks
So with #5157, the call to
_getBoundsCenterZoom
will return the proper center and a zoom ofInfinity
, and together with #4916 everything works for this edge case.