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

Rotating the map does not update its bounds appropriately #2375

Closed
ghost opened this issue Mar 31, 2016 · 6 comments
Closed

Rotating the map does not update its bounds appropriately #2375

ghost opened this issue Mar 31, 2016 · 6 comments

Comments

@ghost
Copy link

ghost commented Mar 31, 2016

The map bounds do not update correctly as you rotate the map.

See demo: https://jsfiddle.net/kmmky7h5/

As you rotate the map in the demo, we expect to see the map bounds to update to the latest lat-long coordinates at the corners of the viewport, but instead, the bounds nest directly on top of each other based on the original bounds.

@ghost ghost changed the title Rotating the map does not update #getBounds appropriately Rotating the map does not update its bounds appropriately Mar 31, 2016
@indus
Copy link
Contributor

indus commented Apr 1, 2016

The bounds you get are defined by only two Points [minLng, minLat] and [maxLng, maxLat] so you can only use them to construct a "north-facing" rectangle that encloses the viewport.

But you can use the 4 unprojected corners of the viewport to construct a real "corner-bound" polygon:

var canvas = map.getCanvas(),
  w = canvas.width,
  h = canvas.height,
  cUL = map.unproject([0,0]).toArray(),
  cUR = map.unproject([w,0]).toArray(),
  cLR = map.unproject([w,h]).toArray(),
  cLL = map.unproject([0,h]).toArray();

  var coordinates = [cUL,cUR,cLR,cLL,cUL];

see: https://jsfiddle.net/kmmky7h5/2/

(Be aware that you may face some problems at ± 180° and towards the poles)

@ghost
Copy link
Author

ghost commented Apr 1, 2016

Awesome! Thanks indus!

Still a +1 to accommodate rotations in map.getBounds

@lucaswoj
Copy link
Contributor

lucaswoj commented Apr 1, 2016

Thanks for the workaround @indus!

Let's merge this discussion with #2112

@lucaswoj lucaswoj closed this as completed Apr 1, 2016
@ghost
Copy link
Author

ghost commented Apr 1, 2016

The only thing is that the corners of the canvas are not exactly the corners of the map; the lower-right coordinate of the canvas goes beyond the map view. But it still works well for the most part.

@chriswhong
Copy link
Contributor

chriswhong commented Dec 22, 2016

Thanks @indus, just used this workaround!

For anyone feeding this into Postgis with ST_GeomFromGeoJSON(), you need to add a crs to your geoJSON, thusly:

    var canvas = this.map.getCanvas(),
    w = canvas.width,
    h = canvas.height,
    cUL = this.map.unproject([0,0]).toArray(),
    cUR = this.map.unproject([w,0]).toArray(),
    cLR = this.map.unproject([w,h]).toArray(),
    cLL = this.map.unproject([0,h]).toArray();

    var polygon = {
      type: 'Polygon',
      coordinates: [[cUL,cUR,cLR,cLL,cUL]],
      "crs":{"type":"name","properties":{"name":"EPSG:4326"}}
    }

@chriswhong
Copy link
Contributor

Here's a blog post that describes using this approach when building a "clip and ship" tool. https://medium.com/nycplanninglabs/getbounds-in-the-era-of-pitch-able-and-rotate-able-maps-4de97f35e64b

Here's a widget that visually shows the difference between the getBounds() rectangle and the actual map extent: https://chriswhong.github.io/mapboxgl-view-bounds/#12.51/40.7768/-73.964/13.6/43
getting_viewport_bounds_in_mapboxgl

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

3 participants