MapView
The main export: a clustered MapView with the same API as
react-native-map-clustering.
import MapView from 'react-native-better-clustering'
import { Marker } from 'react-native-maps'
react-native-better-clustering/compat is a backwards-compatible
alias of the same component.
Basic usage
<MapView
style={{ flex: 1 }}
initialRegion={{
latitude: 52.23,
longitude: 21.01,
latitudeDelta: 0.35,
longitudeDelta: 0.35,
}}
radius={56}
minPoints={2}
clusterColor="#FFCC00"
clusterTextColor="#000000"
>
{points.map((point) => (
<Marker
key={point.id}
coordinate={{
latitude: point.latitude,
longitude: point.longitude,
}}
/>
))}
</MapView>
MapView reads its marker children again whenever they change. It reuses what
it made from a marker element while its key and its position among the
children stay the same, and its props keep their values: each prop the same
value, or a plain object of the same values, such as a new
coordinate={{ latitude, longitude }} literal. So a .map() in render that
creates the same markers again costs a comparison of each one, not a read. It
compares arrays of 64 markers or more; a shorter one is read again, which costs
about as much. A plain object is compared one level deep. A nested object, an
array, a function or an element only matches itself, so a marker given a new
one, such as an inline onPress or a <Callout> child, is read again. When
only some markers change, create the elements of the others once (for example
with useMemo). Put such a group before any list that changes length, since a
list that grows or shrinks ahead of it moves its markers to new positions.
Never change a prop object in place and pass it again, such as a coordinate
whose latitude you set: MapView sees the same object, keeps the point it
made from it, and the marker stays where it was. Pass a new object instead.
Clustering props
All standard react-native-maps MapView props
are supported, plus:
| Prop | Default | Description |
|---|---|---|
radius | ~6% of screen width | Cluster radius in pixels. The default matches react-native-map-clustering and is smaller than the default bubble; for dense data pass about twice it (around 50 on a phone), see Troubleshooting |
minPoints | 2 | Minimum points to form a cluster |
minZoom | 1 | Minimum zoom level |
maxZoom | 20 | Maximum zoom level for clustering |
extent | 512 | Tile extent, in supercluster's sense: radius is in pixels of an extent-wide tile, so a larger extent clusters more tightly at the same radius |
nodeSize | 64 | KD-tree leaf size |
clusteringEnabled | true | Toggle clustering |
spiralEnabled | true | Spread markers that share a location (a cluster that zooming can't split) into a spiral with connector lines. See Markers at the same location |
spiderfyMinZoom | 19 | Camera zoom from which such clusters spread without being pressed (react-native-map-clustering parity) |
spiderfyMaxLeaves | 50 | Most markers a cluster spiderfies into. A larger cluster of overlapping markers stays a bubble; pressing it still calls onClusterPress with every marker, so you can list them instead. Spider markers are spaced about 40 pt apart on screen |
spiderLineColor | #FF0000 | Color of spider connector lines |
clusterColor | #0F52FF | Default cluster bubble color (GMI brand blue) |
clusterTextColor | #FFFFFF | Default cluster label color |
clusterFontFamily | None | Font family for the cluster count label; also passed to renderCluster |
clusterAccessibilityLabel | (count) => `Cluster of ${count} markers` | Accessibility label for a default cluster bubble, passed to its Marker with the button role. Screen readers do not announce it yet: see Accessibility |
animationEnabled | true | Fade and glide the default cluster bubbles on zoom (iOS + Android); see clusterFadeInDuration |
layoutAnimationConf | None | Deprecated and ignored, accepted for react-native-map-clustering compatibility. LayoutAnimation cannot animate map markers on the New Architecture: they are native map annotations outside React Native's layout. Tune the fade with clusterFadeInDuration |
clusterFadeInDuration | 250 | Cross-fade duration (ms) for default cluster bubbles on zoom: new bubbles fade in while removed ones linger and fade out, eliminating the blink from native annotations being added/removed (iOS + Android). Driven by Reanimated on the UI thread; without the optional react-native-reanimated peer, bubbles do not fade and removed ones disappear at once. Bubbles cross-fade when the map settles; during an active pinch/pan, new bubbles appear at once and removed ones stay for up to 100 ms (never longer than this duration) before they go, so a continuous zoom leaves no gaps; at the default clusterUpdateIntervalMs at most one extra generation of bubbles stays mounted. It also times the glide: on a zoom step, a splitting cluster's new bubbles start at its place and glide to theirs, a bubble whose cluster moves slides there, and merging bubbles glide into the one that takes them in (while the map moves, within the 100 ms they stay); pans and data changes only fade. Gated by animationEnabled; ignored with a custom renderCluster. Set to 0 to disable |
tracksViewChanges | false | Forwarded to default cluster markers |
edgePadding | 50px each side | Padding for fitToCoordinates on cluster press, and the room clusterPressZoom 'split' keeps around the markers |
preserveClusterPressBehavior | false | Skip auto fitToCoordinates on cluster press |
clusterPressZoom | 'fit' | How far a cluster press zooms in: 'fit' fits the markers, which zooms all the way in for markers close together; 'split' zooms in only until the cluster splits, and fits when that would not zoom past the split or the cluster does not split by maxZoom. Ignored with preserveClusterPressBehavior and for clusters that spiderfy |
selectedClusterId | None | Highlight a cluster by id |
selectedClusterColor | #FF5722 | Color when cluster is selected |
width / height | window size | Seed map dimensions before onLayout |
clusterUpdateIntervalMs | 100 | Minimum interval (ms) between cluster recomputations while the map is moving; 0 recomputes only on settle. While the camera flies to a pressed cluster, clusters are recomputed only once it settles. On Google Maps, a gesture that takes over the flight resumes the updates above, so the first one can wait up to this interval (until the settle with 0); Apple Maps reports no gestures, so there the flight lasts until the camera settles |
clusterRenderMargin | 100 | How far outside the map, in points, cluster bubbles still render. Clusters are found, and reported to onMarkersChange, for the region plus half its size on each side, as in react-native-map-clustering; only rendering stops at this margin, and markers are not affected. A smaller margin mounts fewer native annotations on each zoom step, which keeps the UI thread free for the bubble fade; a larger one leaves fewer bubbles to appear at the edges during a fast pan. Infinity renders every cluster found |
superClusterRef | None | Receives the loaded Supercluster behind the clusters on screen, or null. It is replaced after each rebuild and the previous engine destroyed, so read current when you query instead of keeping it; see superClusterRef |
mapRef | None | Callback ref to the inner react-native-maps instance |
style applies to a View that wraps the map, and the map fills that view.
radius, minPoints, minZoom, maxZoom, extent and nodeSize must be in
their valid ranges. An invalid one fails like a
failed build (passed to onError, or thrown during render without it), with a
message that names the prop.
Callbacks
| Prop | Signature | Description |
|---|---|---|
onClusterPress | (cluster, markers) => void | Fired when a cluster is tapped, with every marker in it. The markers are read only when you pass this prop; for a cluster of tens of thousands that read is most of what a press costs. The map fits the cluster's bounds either way |
onMarkersChange | (markers) => void | Fired when visible markers/clusters change: once the first index has loaded, then only when the visible set changes. A re-render with the same marker coordinates does not fire it, so onMarkersChange={setState} is safe |
onRegionChangeComplete | (region, details, markers) => void | Extends the maps callback with current markers, in react-native-map-clustering 4.x's order. 3.x passed (region, markers): see Coming from react-native-map-clustering 3.x |
onError | (error) => void | Fired when the native cluster index fails to build, for example because the app was not rebuilt after installing the library. Markers from an earlier successful build keep showing. Without it, the error is thrown during render so an error boundary (and LogBox in development) shows it. See Troubleshooting |
Markers at the same location
Markers at the same coordinate (one building, or geocoding fallbacks) form a
cluster that zooming can never split. With spiralEnabled, MapView spreads
them into a spiral:
- automatically from camera zoom
spiderfyMinZoom(19 by default), and - at any zoom when the cluster is pressed. The map doesn't move; the spiral
collapses when the zoom changes. This also covers maps whose maximum zoom
(
maxZoomLevel, or the provider's limit) sits belowspiderfyMinZoom.
onClusterPress still receives the cluster and its markers. Clusters of
distinct nearby markers keep expanding by zooming, and clusters larger than
spiderfyMaxLeaves stay bubbles. With preserveClusterPressBehavior, a press
only calls onClusterPress.
MapView never clusters at a zoom above maxZoom, so a cluster still there at
maxZoom stays a cluster however far the map zooms in, and spreads out as
above. useClusterer instead returns its markers on their own there (see
Above maxZoom).
Across the antimeridian
When the map spans ±180°, the markers on both sides show. As in supercluster, markers on opposite sides never join one cluster.
Which children are clustered
MapView clusters a child when it is an element with its own coordinate
prop and without cluster={false}, as react-native-map-clustering does. Arrays are flattened, so several
{items.map(…)} lists can sit side by side. Every other child renders as it
is, unclustered, and so does each marker inside it:
<MapView>
{/* Clustered: each child is an element with a `coordinate` prop. */}
{shops.map((shop) => (
<Marker key={shop.id} coordinate={shop.location} />
))}
{cafes.map((cafe) => (
<CafeMarker key={cafe.id} coordinate={cafe.location} cafe={cafe} />
))}
{/* Not clustered: neither the Fragment nor `<ParkPin>` has a `coordinate` prop. */}
<>
{stations.map((station) => (
<Marker key={station.id} coordinate={station.location} />
))}
</>
{parks.map((park) => (
<ParkPin key={park.id} park={park} />
))}
</MapView>
A component such as CafeMarker is clustered by its coordinate prop and
renders as itself when it stands alone, so it must render a Marker at that
coordinate. Spiderfy passes it a new coordinate. Give every marker a stable
key (see Markers flicker on zoom).
Per-marker opt-out
Markers cluster by default (cluster omitted or cluster={true}). Exclude
individual markers from clustering:
<Marker
cluster={false}
coordinate={{ latitude: 52.23, longitude: 21.01 }}
/>
react-native-maps does not declare cluster, so importing from
react-native-better-clustering (or /compat) adds it to the props
TypeScript checks on Marker. It cannot add it to MapMarkerProps, a type
alias, so MapMarkerProps, React.ComponentProps<typeof Marker> and
MarkerAnimated go without it. Type a component that takes cluster and
passes it on to a Marker with MapMarkerProps & { cluster?: boolean }.
Live updates
Moving, adding or removing a clustered marker rebuilds the whole native index on a background thread; the index is never patched. The map keeps showing the previous markers until the new index has loaded. Builds run one at a time: markers that change during a build are built once it has loaded, from the newest children, so markers that move faster than a build loads update on the map once per build.
Any change to the children, even to a marker that does not cluster, makes
MapView look for the markers that changed on the JS thread, to tell whether
the index needs rebuilding. An array of markers it has read before, at the same
place among the children, costs a comparison of its elements rather than a read
of each marker. A rebuild then also packs every clustered marker for the native
build, whose time grows with their number. To keep live updates cheap:
- Create the elements of markers that do not move once (with
useMemo), and pass them as a child of their own ({staticMarkers}) rather than spread into one array with the moving markers, so each update reads only the moving ones. - Give moving markers that do not need to cluster
cluster={false}. They skip the index, so moving them never rebuilds it.
The example app's Live fleet scenario measures both, with up to 50,000 static markers and vehicles that move up to 10 times a second.
Custom cluster UI
Pass renderCluster for full control over the cluster bubble:
import MapView, {
type RenderClusterProps,
} from 'react-native-better-clustering'
import { Marker } from 'react-native-maps'
const renderCluster = (cluster: RenderClusterProps) => {
const [longitude, latitude] = cluster.geometry.coordinates
return (
<Marker
coordinate={{ latitude, longitude }}
onPress={cluster.onPress}
tracksViewChanges={cluster.tracksViewChanges}
>
{/* your cluster bubble */}
</Marker>
)
}
<MapView renderCluster={renderCluster} {...props}>
{/* markers */}
</MapView>
Cluster features include properties.point_count,
properties.point_count_abbreviated, and
properties.getExpansionRegion() for zoom-on-tap.
Accessibility
Each default cluster bubble passes an accessibilityLabel and the button
role to its Marker. The label is Cluster of N markers, or what
clusterAccessibilityLabel returns for the number of markers:
<MapView
clusterAccessibilityLabel={(count) => t('map.cluster', { count })}
{...props}
>
{/* markers */}
</MapView>
Screen readers do not announce it yet. react-native-maps (up to 1.29) does
not pass a Marker's accessibilityLabel or accessibilityRole to the
native map annotation: on iOS with Apple Maps, VoiceOver reads a cluster
bubble as "Map pin", and on Android the label does not reach the Google Maps
marker. A renderCluster marker has the same limit. The bubbles announce
their label once react-native-maps passes it on; follow
react-native-maps#6046.
Types
import type {
ClusteredMapViewProps,
RenderClusterProps,
} from 'react-native-better-clustering'
ClusteredMapViewProps is the full props type for the clustered MapView.