Integrating Mapbox's Supercluster with react-map-gl
Hello! As my title suggests, since mapbox-gl already supports supercluster internally with newly updated spiderfying and other cool jazz, I am wondering if there is a best practice or if anyone has had the experience implementing a clustering layer within react-map-gl wrapper. Or something similar to this?
@winston-bosan if still relevant I manage to do it. Maybe will send a PR here
@jamalx31 how did you go about implementing it?
@jamalx31 I'd also be curious. This seems like it would be something nice to have in this library. If you're not keen on PRing, can you at least share the implementation so that someone else could go about PRing it?
@mlg87 @tstirrat15 I will try to raise a PR this week if not I will share a code snippet maybe one of you could
Sorry a bit busy and will take me longer but her is the code for the Cluster component and how to use it:
import supercluster from 'supercluster';
import { point } from '@turf/helpers';
import { Children, PureComponent, createElement } from 'react';
import PropTypes from 'prop-types';
// import type { Node, Component } from 'react';
import { Marker } from 'react-map-gl';
const childrenKeys = children =>
Children.toArray(children).map(child => child.key);
const shallowCompareChildren = (prevChildren, newChildren) => {
if (Children.count(prevChildren) !== Children.count(newChildren)) {
return false;
}
const prevKeys = childrenKeys(prevChildren);
const newKeys = new Set(childrenKeys(newChildren));
// console.log(prevKeys.length === newKeys.size && prevKeys.every(key => newKeys.has(key)), {prevChildren, newChildren})
return (
prevKeys.length === newKeys.size && prevKeys.every(key => newKeys.has(key))
);
};
class Cluster extends PureComponent {
static displayName = 'Cluster';
static defaultProps = {
minZoom: 0,
maxZoom: 16,
radius: 40,
extent: 512,
nodeSize: 64,
};
constructor(props) {
super(props);
this.state = {
clusters: [],
};
}
componentDidMount() {
this.createCluster(this.props);
this.recalculate();
this.props.map.on('moveend', this.recalculate);
}
componentWillReceiveProps(newProps) {
const shouldUpdate =
newProps.minZoom !== this.props.minZoom ||
newProps.maxZoom !== this.props.maxZoom ||
newProps.radius !== this.props.radius ||
newProps.extent !== this.props.extent ||
newProps.nodeSize !== this.props.nodeSize ||
!shallowCompareChildren(this.props.children, newProps.children);
if (shouldUpdate) {
this.createCluster(newProps);
this.recalculate();
}
}
createCluster = props => {
const {
minZoom,
maxZoom,
radius,
extent,
nodeSize,
children,
innerRef,
} = props;
const cluster = supercluster({
minZoom,
maxZoom,
radius,
extent,
nodeSize,
});
const points = Children.map(children, child => {
if (child)
return point([child.props.longitude, child.props.latitude], child);
return null;
});
cluster.load(points);
this.cluster = cluster;
if (innerRef) innerRef(this.cluster);
};
recalculate = () => {
const zoom = this.props.map.getZoom();
const bounds = this.props.map.getBounds().toArray();
const bbox = bounds[0].concat(bounds[1]);
const clusters = this.cluster.getClusters(bbox, Math.floor(zoom));
this.setState(() => ({ clusters }));
};
render() {
const clusters = this.state.clusters.map(cluster => {
if (cluster.properties.cluster) {
const [longitude, latitude] = cluster.geometry.coordinates;
return createElement(Marker, {
longitude,
latitude,
// TODO size
offsetLeft: -28 / 2,
offsetTop: -28,
children: createElement(this.props.element, {
cluster,
superCluster: this.cluster,
}),
key: `cluster-${cluster.properties.cluster_id}`,
});
}
const { type, key, props } = cluster.properties;
return createElement(type, { key, ...props });
});
return clusters;
}
}
Cluster.propTypes = {
/** Mapbox map object */
map: PropTypes.object,
/** Minimum zoom level at which clusters are generated */
minZoom: PropTypes.number,
/** Maximum zoom level at which clusters are generated */
maxZoom: PropTypes.number,
/** Cluster radius, in pixels */
radius: PropTypes.number,
/** (Tiles) Tile extent. Radius is calculated relative to this value */
extent: PropTypes.number,
/** Size of the KD-tree leaf node. Affects performance */
nodeSize: PropTypes.number,
/** ReactDOM element to use as a marker */
element: PropTypes.func,
/**
* Callback that is called with the supercluster instance as an argument
* after componentDidMount
*/
/* eslint-disable react/no-unused-prop-types */
innerRef: PropTypes.func,
/* eslint-enable react/no-unused-prop-types */
/** Markers as children */
children: PropTypes.node,
};
export default Cluster;
and use it like this:
<ReactMapGL
{...viewport}
// eslint-disable-next-line no-return-assign
ref={ref => (this.mapRef = ref)}
onLoad={() => this.setState({ map: this.mapRef.getMap() })}
mapStyle="mapbox://styles/mapbox/dark-v9"
mapboxApiAccessToken={TOKEN}
onViewportChange={onViewportChange}
>
{map && (
<Cluster
map={map}
radius={20}
extent={512}
nodeSize={40}
element={clusterProps => (
<PinGroup onViewportChange={onViewportChange} {...clusterProps} />
)}
>
{/* every item should has a
uniqe key other wise cluster will not rerender on change */}
{/* points.map((point, i) => (
<Marker
key={i}
longitude={point.longitude}
latitude={point.latitude}
>
<div style={MarkerStyle} />
</Marker>
)) */}
</Cluster>
)}
<MapPopup />
</ReactMapGL>
@jamalx31 this is awesome. Thank you!
Hmm... It's interesting to me that this doesn't use the underlying cluster functionality exposed by mapbox-gl-js. This is putting clusters on top of markers, more or less, where it seems like the more direct way to go would be to add layers via the mapStyle attribute, a la this example, though using something more like the cluster example on the mapbox docs.
Has this feature, as described by @tstirrat15 , made it into react-map-gl?
@JasonLunsford Did you find a solution for this? Would appreciate any help
For everybody getting errors -> supercluster was updated you now have to call new Supercluster instead of supercluster.
import Supercluster from 'supercluster';
const cluster = new Supercluster({
minZoom,
maxZoom,
radius,
extent,
nodeSize,
});
Also, it might be good for some people to know that PinGroup is a component you have to build and that it will be the replacement for the other pins when the clustering is happening. Just to make this clear as it wasn't clear to me from the beginning.
For everybody getting errors -> supercluster was updated you now have to call new Supercluster instead of supercluster.
import Supercluster from 'supercluster'; const cluster = new Supercluster({ minZoom, maxZoom, radius, extent, nodeSize, });Also, it might be good for some people to know that PinGroup is a component you have to build and that it will be the replacement for the other pins when the clustering is happening. Just to make this clear as it wasn't clear to me from the beginning.
Hi, thank you: could you please give me an example of a PinGroup component?
Using @jamalx31 's code as reference:
componentDidMount() { this.createCluster(this.props); this.recalculate(); this.props.map.on('moveend', this.recalculate); }
The this.props.map.on('moveend', this.recalculate); gives me a warning:
Warning: Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state.
What should I change to fix it but having the same result?
For everybody getting errors -> supercluster was updated you now have to call new Supercluster instead of supercluster.
import Supercluster from 'supercluster'; const cluster = new Supercluster({ minZoom, maxZoom, radius, extent, nodeSize, });Also, it might be good for some people to know that PinGroup is a component you have to build and that it will be the replacement for the other pins when the clustering is happening. Just to make this clear as it wasn't clear to me from the beginning.
Hi, thank you: could you please give me an example of a PinGroup component?
import React, { Component } from 'react'
class PinGroups extends Component {
constructor(props) {
super(props)
}
render() {
const { cluster } = this.props;
return(
<div>
{cluster.properties.point_count}
</div>
)
}
}
export default PinGroups;
Super basic PinGroup component with the number of markers in the cluster
Is there an official support for clusters now? This is a must-have feature for any map library IMHO.
Any update on this? I was kind of surprised that this isn't officially supported yet...
Should we create a pull request and add the cluster component of @jamalx31 ? 🤔
Bump.
I am considering going to raw mapboxgl.js now because of the lack of cluster support. We have a lot of data, spread across the whole world. Not being able to cluster it efficiently, just doesn't make sense in our use-case.
@derwaldgeist https://uber.github.io/react-map-gl/#/Examples/clusters
@Pessimistress , https://uber.github.io/react-map-gl/#/Examples/clusters -> in this example, it clusters Points layer. How about clustering custom markers? It is possible to execute it using this package?
@Pessimistress @jamalx31 This isn't working code, just what I spent several hours on. A potentially-workable concept to help react-map-gl provide "cluster" support in your source, unless there is no way to access the marker with DOM... then forget this
@howdyhyber If by custom markers you mean image markers, I have custom markers as well. Since clustering only works on layers in latter example (and the former example in this thread uses mapbox-js-gl) I tried to use refs & also tried document.getElementByID, to see if markers overlap, then hide all overlaps or re-render when viewport changes from last this.setState({lastViewport: this.state.viewport}) (probably not very performant but didn't get it working anyway). It seems we don't have DOM access to the Markers once rendered to do so.
Here is inside componentDidUpdate
if (
this.state.mounted &&
this.props.data &&
this.state.lastViewport !== this.state.viewport
) {
this.setState({ lastViewport: this.state.viewport });
this.props.data.map(d => {
console.log("level1");
// Div 1 data
var d1_offset = document.getElementById("x" + d.id).offset(); //fails here
var d1_height = document.getElementById("x" + d.id).outerHeight(true);
var d1_width = document.getElementById("x" + d.id).outerWidth(true);
var d1_distance_from_top = d1_offset.top + d1_height;
var d1_distance_from_left = d1_offset.left + d1_width;
this.props.data.map(g => {
console.log("level2");
// Div 2 data
var d2_offset = document.getElementById("x" + g.id).offset();
var d2_height = document.getElementById("x" + g.id).outerHeight(true);
var d2_width = document.getElementById("x" + g.id).outerWidth(true);
var d2_distance_from_top = d2_offset.top + d2_height;
var d2_distance_from_left = d2_offset.left + d2_width;
var not_colliding =
d1_distance_from_top < d2_offset.top ||
d1_offset.top > d2_distance_from_top ||
d1_distance_from_left < d2_offset.left ||
d1_offset.left > d2_distance_from_left;
console.log("ran here");
// Return whether it IS colliding
let x = 0;
if (!not_colliding) {
x++;
}
if (x > 1) {
console.log("overlapping");
ReactDOM.findDOMNode(g).style.display = "none";
d.next.addEventListener(
"click",
() => {
this.props.zoomIn(); //This re-renders my query && zooms in by 3...
},
false
);
}
})
})
}
}
}
Please let me know if I'm wrong and it is possible to use refs or getElementById on markers, but I just read a similar question of Leaflet which say it is NOT possible for them