mapbox / mapbox-gl-js

Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
https://docs.mapbox.com/mapbox-gl-js/
Other
11.18k stars 2.22k forks source link

Fit view to a 3D axis-aligned bounding box #11962

Open brncsk opened 2 years ago

brncsk commented 2 years ago

Motivation

For an indoor visualization use case, I'd like to be able to fit the map view to a building's 3D bounds – see the following screenshot:

image

I cannot currently do that, as fitBounds() only lets me fit 2D bounding boxes.

Design

AFAICT the core functionality is already there in the form of Camera#_cameraForBox() – but it's only used for fitting an elevation box at non-zero pitch ATM.

Possible options I can think of:

  1. Expose minAltitude / maxAltitude directly in FitBoundsOptions.

  2. Extend LngLatBounds / LngLatBoundsLike to accept three-tuples of (lng, lat, alt) – where alt is in meters. (I guess this is both the more involved and the more questionable API design choice as the vertical unit of measurement differs from the horizontal ones.)

  3. As the ray marching algorithm iteratively changes the transform's center, zoom, pitch and bearing, it may be better to expose this functionality as a new method on Camera (and transitively on Map), similarly to how fitScreenCoordinates() (the only call-site for _cameraForBox()) is also a distinct method.

Mock-Up

(Original code taken from https://docs.mapbox.com/mapbox-gl-js/example/fitbounds/).

Option 1 (exposing minAltitude / maxAltitude in FitBoundsOptions):

map.fitBounds([
  [32.958984, -5.353521], // southwestern corner of the bounds
  [43.50585, 5.615985], // northeastern corner of the bounds
  {
    minAltitude: 0,
    maxAltitude: 100
  }
]);

Option 2 (LngLatBoundsLike accepts three-tuples):

map.fitBounds([
  [32.958984, -5.353521, 0], // southwestern corner of the bounds (at 0 altitude)
  [43.50585, 5.615985, 100], // northeastern corner of the bounds (at 100m altitude)
]);

Option 3 (expose a new method on Camera and Map):

map.fitBounds3D(
  [32.958984, -5.353521], // southwestern corner of the bounds
  [43.50585, 5.615985], // northeastern corner of the bounds
  {
    minAltitude: 0,
    maxAltitude: 100
  }
)

Would you accept a PR once the design is decided on?

SnailBones commented 2 years ago

Thanks for opening this issue @brncsk!

Would fitScreenCoordinates work for your use case? Due to perspective this wouldn't work in all cases, but if the camera position is close to the goal position, then calculating the screen bounding box of your building would likely give a decent approximation.

If you do proceed with the PR we'd be happy to accept it! I personally lean toward option 2.