mapboxGetActualMapBounds

Gitlab
A utility function that retrieves the actual map bounds ignoring any padding set on the map.
Added since: v0.1.0
Last changed:

Usage

A utility function that calculates the true viewport bounds of a Mapbox GL map instance, ignoring any padding that might be set on the map. This is particularly useful when you need to work with the actual visible area of the map without padding adjustments.

Basic example

MapComponent.vue
<script setup lang="ts">
import { mapboxGetActualMapBounds } from '@nobears-front-end/nuxt-mapbox';

const map = new mapboxgl.Map({ ... });
const bounds = mapboxGetActualMapBounds(map, 50);
console.log(bounds); // [-180, -85, 180, 85] or actual bounds if map exists
</script>

Type Definition

declare type 
MapboxMapBounds
= [number, number, number, number];
const
DEFAULT_WORLD_BOUNDS
:
MapboxMapBounds
= [-180, -85, 180, 85] as
const
;
declare function
mapboxGetActualMapBounds
(
map
:
Map
| null,
padding
?: number
):
MapboxMapBounds
;

API Reference

Calculates the actual map bounds ignoring any padding set on the map.

Params

map
Map | null required
The Mapbox GL map instance. If null, returns the default world bounds.
padding
number
Optional padding in pixels to add to the bounds.
Default: 0

Changelog