mapboxExpandCluster

Gitlab
A utility function for expanding Mapbox clusters by zooming to fit all contained markers within the viewport.
Added since: v0.1.0
Last changed:

Usage

mapboxExpandCluster() is a utility that enhances the default Mapbox cluster expansion behavior. While the default behavior only zooms enough to break up a cluster, this function zooms in as far as possible while ensuring all markers within the cluster remain visible in the viewport.

Behavior for small maps (width < 640px):
This is the fallback behavior, since it most likely returns the same result as the default zoom.

  1. Gets the zoom level from the getClusterExpansionZoom() method of the Supercluster instance. (See Supercluster API)
  2. Centers the map on the cluster coordinates using the flyTo() method of the Map instance.

Behavior for larger maps:
This is the intended behavior.

  1. Recursively retrieves all markers within the cluster using the getLeaves() method of the Supercluster instance. (See Supercluster API)
  2. Creates a bounding box containing all markers and fits the viewport to show all markers while maximizing the zoom level.

Basic example

MapComponent.vue
<template>
  <button @click="mapboxExpandCluster(map, supercluster, clusterFeature)">
    Expand Cluster
  </button>
</template>

Type Definition

declare function 
mapboxExpandCluster
(
map
:
Map
| null,
supercluster
:
Supercluster
,
feature
:
Feature
<
Point
, {
cluster_id
: number }>,
options
:
EasingOptions
= {
padding
: 100 },
): void;

API Reference

Params

map
Map | null required
The Mapbox GL map instance.
supercluster
Supercluster required
The Supercluster instance used for managing the clusters.
feature
Feature<Point, { cluster_id: number }> required
The GeoJSON feature representing the cluster to expand.
options
EasingOptions
Optional easing options for the map animation.

Changelog

v0.2.3

on

#ebea626c

-

improvement: added full typescript support