A Clustering-enabled map for React Native
npm install react-native-maps-super-cluster
npm i --save react-native-maps-super-clusterNOTES:
* the prop key of the markers rendered through renderMarker should not be left up to React. Instead, we strongly suggest to use an id in order the have unique keys while still taking advantage of React's recycling
* ClusteredMapView supports usual React children. Those children won't be affected by clustering, i.e. the behavior for those children is exactly the same as wrapping them around an AirBnB's react-native-maps instance
* Use onMarkerPress event on MapView instead of using onPress directly on Markers whenever possibile, in particular if you have a lot of pins and clusters. Within onMarkerPress you have access to the marker identifier through the event.nativeEvent attribute, hence you should be able to do everything you would do within an onPress function of a Marker
``JSX
import React, { Component } from 'react'
import { Marker, Callout } from 'react-native-maps'
import ClusteredMapView from 'react-native-maps-super-cluster'
const INIT_REGION = {
latitude: 41.8962667,
longitude: 11.3340056,
latitudeDelta: 12,
longitudeDelta: 12
}
export default class MyClusteredMapView extends Component {
...
renderCluster = (cluster, onPress) => {
const pointCount = cluster.pointCount,
coordinate = cluster.coordinate,
clusterId = cluster.clusterId
// use pointCount to calculate cluster size scaling
// and apply it to "style" prop below
// eventually get clustered points by using
// underlying SuperCluster instance
// Methods ref: https://github.com/mapbox/supercluster
const clusteringEngine = this.map.getClusteringEngine(),
clusteredPoints = clusteringEngine.getLeaves(clusterId, 100)
return (
{pointCount}
{
/*
Eventually use
show clustered point thumbs, i.e.:
{
clusteredPoints.map(p => (
))
}
IMPORTANT: be aware that Marker's onPress event isn't really consistent when using Callout.
*/
}
)
}
renderMarker = (data) =>
...
render() {
return (
data={this.state.data}
initialRegion={INIT_REGION}
ref={(r) => { this.map = r }}
renderMarker={this.renderMarker}
renderCluster={this.renderCluster} />
)
}
}
`
Name | Type | Required | Default | Note
---------|----------|--------------|-------------|---------
radius | Number | false | window width * 4,5% | SuperCluster radius.
extent | Number | false | 512 | SuperCluster extent.
minZoom | Number | false | 1 | SuperCluster minZoom.
maxZoom | Number | false | 16 | SuperCluster maxZoom.
width | Number | false | window width | map's width.
height | Number | false | window height | map's height.
data | Array
#### features
* improve scaleUpRatio` math for fontSize
* trigger events on cluster implode/explode
* Leonardo Lusoli
* Alberto Dallaporta