question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Crash when fitBounds() to a zero-area bounds on uninitialized map

See original GitHub issue

How 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

https://jsfiddle.net/emirdeliz/27a1hrvy/

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
emirdelizcommented, Dec 1, 2016

Really, this fixed the problem after set maxZoom=15. Thanks

0reactions
IvanSanchezcommented, Dec 1, 2016

So with #5157, the call to _getBoundsCenterZoom will return the proper center and a zoom of Infinity, and together with #4916 everything works for this edge case.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found