Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid Lat-Long range from map#getBounds over certain map areas #3690

Closed
ghost opened this issue Nov 23, 2016 · 8 comments
Closed

Invalid Lat-Long range from map#getBounds over certain map areas #3690

ghost opened this issue Nov 23, 2016 · 8 comments

Comments

@ghost
Copy link

ghost commented Nov 23, 2016

On mapbox-gl-js v0.28.0:

Steps to Trigger Behavior

Go to https://jsfiddle.net/ecLwcn7n/

In the geocoder, search "Japan".

Expected Behavior

The LatLng range from calling map.getBounds() should be within range of valid latitude (+/- 90), longitude (+/- 180) values

Actual Behavior

The LatLng range

sw: LngLat(-251.2313600037629, 20.32309528499414)
ne: LngLat(-191.84655553831513, 45.6228867310015)

is invalid. Longitude, in particular, is off.

Moreover, on v0.27.0, the output of map#getBounds over the same map area produces different (still invalid) values.

sw: LngLat(-239.0949959093802, 20.323095284897974)
ne: LngLat(-203.98291963402065, 45.62288673102569)

See https://jsfiddle.net/ecLwcn7n/1/ for v0.27

@jfirebaugh
Copy link
Contributor

Mapbox GL JS does not currently wrap the map center longitude when panning across the antimeridian, so getBounds() is actually expected to return values outside of +/- 180 when viewing copies of the world left or right of the antimeridian. Changing this behavior is tracked in #2071.

@jpdevries
Copy link

Does anyone know if there are any more examples or documentation on these -90/90 coordinates? I'm trying to fit the map to
[[37.2627693040104, -121.95691258908977], [37.77271424601054, -122.41830437057172]]

and am completely lost and getting an "Invalid LngLat latitude value: must be between -90 and 90" error but I don't understand why those coordinates don't work (they work with zoom and map pins)

@lbud
Copy link
Contributor

lbud commented Jun 15, 2017

@jpdevries your coordinates look like [lat, lng] — does it work if you flip those?

@jpdevries
Copy link

jpdevries commented Jun 15, 2017

@lbud unfortunately no. If I flip them I get Error: failed to invert matrix.

I don't understand, does the flyTo fitBounds use a totally different type of coordinates?

I've read the docs here but they don't indicate anything about "different coordinates" needing to be used. I just wanna fly to San Francisco 😭 lol

@lbud
Copy link
Contributor

lbud commented Jun 15, 2017

The documentation for LngLatBoundsLike indicates that coordinates should be [lng, lat] coordinates in [sw, ne] order. Your coordinates appear to be SE, NW. The following should work:

map.fitBounds([
    [-122.41830437057172, 37.2627693040104], 
    [-121.95691258908977, 37.77271424601054]
]);

If that still throws Error: failed to invert matrix, that's likely a bug.

Hope that helps. In the future if you have questions about usage, we recommend asking on StackOverflow.

@jpdevries
Copy link

Thanks @lbud! I thought maybe I found a bug but I must of misunderstood the API. Given my two points, the south-most is also the east-most, so I think I just need to do some clever stuff before calling fitBounds().

Screenshot

@remidej
Copy link

remidej commented May 13, 2020

Had the same issue, I fixed it using the modulo operator

const validLat = invalidLat % 85;
const validLng = invalidLng % 180

@igorbrandaodev
Copy link

igorbrandaodev commented Sep 5, 2022

I've ran into some problems because of getBounds() method returning invalid numbers. I ended up with this solution, in case anyone got here in the future:


let coordinates = [
  -180.95167287101458,
  -10.741049130205468,
  -120.84336316442753,
  41.92383161264959
]

coordinates .forEach((x, index, array) => {

  isEven = (index % 2) === 0;

  if ((x > 180 || x < -180) && isEven) {
    console.log('invalid long' + index);

    if (x > 0)
      array[index] = 180
    else
      array[index] = -180;
  }

  if ((x > 90 || x < -90) && !isEven) {
    console.log('invalid lat' + index);

    if (x > 0)
      array[index] = 90
    else
      array[index] = -90;
  }
});


// Log to console
console.log(coordinates )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants