mapboxFindExpansionZoomForFeature

Gitlab
A utility function that finds the minimum zoom level needed to break a feature out of its cluster in Mapbox GL JS.
Added since: v0.1.0
Last changed:

Usage

mapboxFindExpansionZoomForFeature() is a utility function that helps determine the exact zoom level at which a specific feature will become visible as an individual point rather than being part of a cluster. This is particularly useful when you want to smoothly zoom to a specific feature that might be contained within a cluster.

Behavior for unclustered features:
The function works by recursively traversing the cluster hierarchy:

  1. First checks if the feature is already visible at the current zoom level
  2. If not, finds the cluster containing the feature
  3. Gets the expansion zoom for that cluster
  4. Recursively checks if the feature is visible at that zoom level
  5. Continues until either:
    • The feature is found at the current level
    • No containing cluster is found
    • The maximum zoom level is reached

Basic example

MapComponent.vue
<script setup lang="ts">
import { mapboxFindExpansionZoomForFeature } from '@nobears-front-end/nuxt-mapbox';
import type { Feature, Point } from "geojson";
import Supercluster from "supercluster";

const supercluster = new Supercluster({ ... });
const features = [
  { type: 'Feature', id: 'cluster1', properties: { cluster: true } },
  { type: 'Feature', id: 'point1', properties: { cluster: false } }
];

const zoom = mapboxFindExpansionZoomForFeature(supercluster, features, 'point1', 10);
</script>

Type Definition

declare function 
mapboxFindExpansionZoomForFeature
(
index
:
Supercluster
,
features
:
Array
<
Feature
<
Point
, {
cluster
?: boolean }>>,
id
: string | number | undefined,
currentZoom
: number,
): number;

API Reference

Params

index
Supercluster required
The Supercluster instance containing the feature and cluster data.
features
Array<Feature<Point, { cluster?: boolean }>> required
Array of GeoJSON features to search through, typically containing clusters.
id
string | number | undefined required
The ID of the feature to find the expansion zoom for. If undefined or not found, returns currentZoom.
currentZoom
number required
The current zoom level of the map.

Return Value

number
The minimum zoom level needed to break the feature out of its cluster, or currentZoom if not found.

Changelog

v0.2.3

on

#ebea626c

-

improvement: added full typescript support

v0.1.0

on

#5aa3607d

-

improvement: added missing utils that where already used in projects