mapbox / mapbox/mapbox-gl-js

Add a padding option to map.getBounds

Open
#9,265 4 comments 14 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

## Motivation

### Scenario:
- Various DOM elements on the web page outside of the map are associated with geographical coordinates
- When hovering over these elements, a marker should appear on the map
- The map bounds potentially need to expand on mouse entering an element to accommodate markers outside of the current viewport. On mouse leaving an element, the map bounds then contract back to some pre-defined reference frame.
- If the user has scrolled or zoomed the map, this pre-defined reference frame is updated to reflect the bounds when scrolling or zooming is complete so that on mouse leave, the map returns to the last position the user set, whilst still allowing temporary expansion to show extra markers whilst hovering on the respective DOM elements

### Problem:
- The user's desired bounds are stored with a call to map.getBounds()
- The expanded view is set with a call to map.fitBounds(..., { padding: 20 }) so that markers are clearly visible in the viewport
- This means that any call to fitBounds() after the call to getBounds() will expand the viewport by 20px, even if the marker falls within the current map bounds.

Without knowing how the extra 20px padding is added on in fitBounds(), it would be difficult to adjust this logic to prevent any zoom and scroll between getBounds and fitBounds calls.

## Design Alternatives

- The padding argument could be a global map setting. All calls to getting and setting bounds take it in to consideration.
- getBounds could take an optional padding argument, which is subtracted from the actual bounds before being returned.

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 by tracing the public map.getBounds and fitBounds entry points, focusing on how fitBounds currently handles its padding option. Compare the two proposed API designs and establish the expected behavior for saving, expanding, and restoring bounds. Done means one design is selected, implemented, and verified for padded and unpadded bounds behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.