mapboxFocusSingleFeature() is a utility function that helps focus the map view on a specific GeoJSON feature. It intelligently handles both clustered and non-clustered features, automatically calculating the appropriate zoom level needed to break clustered features out of their clusters.
Behavior for unclustered features:
When the feature isn't clustered, simply fly to it.
map.flyTo() to center the map on the feature's coordinatesBehavior for clustered features:
When the feature is clustered, zoom to the minimum expansion zoom needed to break this feature out of the cluster.
<script setup lang="ts">
import { mapboxFocusSingleFeature } from '@nobears-front-end/nuxt-mapbox';
// Focus on a non-clustered feature
mapboxFocusSingleFeature(map, supercluster, feature);
// Focus on a clustered feature with custom animation options
mapboxFocusSingleFeature(map, supercluster, feature, {
duration: 1000,
easing: (t) => t * (2 - t)
});
</script>
declare function mapboxFocusSingleFeature(
map: Map | null,
supercluster: Supercluster,
feature: Feature<Point, { cluster?: boolean }>,
options?: EasingOptions,
): void;