mapbox / mapbox/mapbox-gl-js

Fit view to a 3D axis-aligned bounding box

Open
#11,962 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

auto-triaged feature :green_apple:
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/ui/camera.js, especially Camera#_cameraForBox(), and compare its existing fitScreenCoordinates() call path with fitBounds() and FitBoundsOptions. Decide the public API for 3D axis-aligned bounds, then verify that the selected entry point fits the requested minimum and maximum altitude around the building view.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, frontend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.