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.

fitBounds breaks when bounds 'nw' === 'se'

See original GitHub issue

let’s say the bounds for fitBounds is:

{ "nw": { "lat": 33.76845, "lng": -84.3883053 }, "se": { "lat": 33.76845, "lng": -84.3883053 } }

fitBounds will return { "zoom": 0, "center": { "lat": 33.76844999999999, "lng": 95.61169469999999 } }

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:10
  • Comments:8

github_iconTop GitHub Comments

1reaction
henrymoewscommented, Jun 27, 2018

@magicalbanana Sure. In the end we had a function that got the nw and se as a param and we made sure those to points weren’t identically by adding / substracting a tiny number. Here’s the complete function:

/**
 *
 *
 @param mapSize
 * @param bounds {{nw: {lat: number, lng: number}, se: {lat: number, lng: number}}}
 * @returns {{center: {lat: number, lng: number}, zoom: number}}
 */
export function getMapCenterAndZoomByBounds(mapSize, bounds){

  // the fitBounds method cannot handle bounds that have the same nw and se values so let's modify them a bit
  const boundsToUse = bounds;
  if(bounds.nw.lat === bounds.se.lat){
    boundsToUse.se.lat += 0.0000001;
  }
  if(bounds.nw.lng === bounds.se.lng){
    boundsToUse.se.lng += 0.0000001;
  }

  const centerAndZoom = fitBounds(boundsToUse, mapSize);

  // Just a fallback; there is no zoom if the coords given as NW and SE weren't actually NW and SE
  if(!centerAndZoom.zoom){
    centerAndZoom.zoom = 17;
  }

  return {
    center: centerAndZoom.center,
    zoom: Math.min(centerAndZoom.zoom, MAX_DEFAULT_ZOOM) - 1,
  };
}
1reaction
lancetwcommented, Sep 21, 2016

I got similar issues with onChange callback, the correct longitude is -121.8863286, but center return 238.11366940000005.

{center: {lat: 37.33820819999998, lng: 238.11366940000005}, zoom: 16, bounds: {nw: {lat: 37.34186755251527, lng: -121.89283227465205}, se: {lat: 37.33454866918679, lng: -121.87982892534785}}, marginBounds: {nw: {lat: 37.34186755251527, lng: -121.89283227465205}, se: {lat: 37.33454866918679, lng: -121.87982892534785}}, size: {width: 606, height: 429}} 39

Read more comments on GitHub >

github_iconTop Results From Across the Web

Leaflet - map.fitBounds() / map.getBoundsZoom() BROKEN ...
But fitBounds seems to be broken after resizing the map. Same problem with map.getBoundsZoom(). To reproduce the buggy behaviour, start with a ...
Read more >
Fit a map to a bounding box | Mapbox GL JS
This example zooms and pans the map so the new visible area of the map fits within the specified geographical bounds.
Read more >
Documentation - a JavaScript library for interactive maps
Sets a map view that contains the given geographical bounds with the maximum zoom level possible. fitWorld(<fitBounds options> options?) this. Sets a map...
Read more >
L.LatLngBounds
Method Returns Description getSouthWest() LatLng Returns the south‑west point of the bounds. getNorthEast() LatLng Returns the north‑east point of the bounds. getNorthWest() LatLng Returns the north‑west...
Read more >
map.fitBounds(map.getBounds()) zooms out map - Issue Tracker
Another solution would be that getBounds() returns the bounds inside the automatically added margins (but this could break existing applications) ...
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