mirror of
https://github.com/visgl/react-map-gl.git
synced 2026-01-25 16:02:50 +00:00
76 lines
1.9 KiB
JavaScript
76 lines
1.9 KiB
JavaScript
import * as React from 'react';
|
|
import {useState, useRef} from 'react';
|
|
import {render} from 'react-dom';
|
|
import MapGL, {Source, Layer} from 'react-map-gl';
|
|
|
|
import ControlPanel from './control-panel';
|
|
import {clusterLayer, clusterCountLayer, unclusteredPointLayer} from './layers';
|
|
|
|
const MAPBOX_TOKEN = ''; // Set your mapbox token here
|
|
|
|
export default function App() {
|
|
const [viewport, setViewport] = useState({
|
|
latitude: 40.67,
|
|
longitude: -103.59,
|
|
zoom: 3,
|
|
bearing: 0,
|
|
pitch: 0
|
|
});
|
|
const mapRef = useRef(null);
|
|
|
|
const onClick = event => {
|
|
const feature = event.features[0];
|
|
const clusterId = feature.properties.cluster_id;
|
|
|
|
const mapboxSource = mapRef.current.getMap().getSource('earthquakes');
|
|
|
|
mapboxSource.getClusterExpansionZoom(clusterId, (err, zoom) => {
|
|
if (err) {
|
|
return;
|
|
}
|
|
|
|
setViewport({
|
|
...viewport,
|
|
longitude: feature.geometry.coordinates[0],
|
|
latitude: feature.geometry.coordinates[1],
|
|
zoom,
|
|
transitionDuration: 500
|
|
});
|
|
});
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<MapGL
|
|
{...viewport}
|
|
width="100%"
|
|
height="100%"
|
|
mapStyle="mapbox://styles/mapbox/dark-v9"
|
|
onViewportChange={setViewport}
|
|
mapboxApiAccessToken={MAPBOX_TOKEN}
|
|
interactiveLayerIds={[clusterLayer.id]}
|
|
onClick={onClick}
|
|
ref={mapRef}
|
|
>
|
|
<Source
|
|
id="earthquakes"
|
|
type="geojson"
|
|
data="https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson"
|
|
cluster={true}
|
|
clusterMaxZoom={14}
|
|
clusterRadius={50}
|
|
>
|
|
<Layer {...clusterLayer} />
|
|
<Layer {...clusterCountLayer} />
|
|
<Layer {...unclusteredPointLayer} />
|
|
</Source>
|
|
</MapGL>
|
|
<ControlPanel />
|
|
</>
|
|
);
|
|
}
|
|
|
|
export function renderToDom(container) {
|
|
render(<App />, container);
|
|
}
|