项目作者: urbica

项目描述 :
Urbica React Cluster Component for Mapbox GL JS
高级语言: JavaScript
项目地址: git://github.com/urbica/react-map-gl-cluster.git
创建时间: 2019-03-11T14:14:31Z
项目社区:https://github.com/urbica/react-map-gl-cluster

开源协议:MIT License

下载


Urbica React Mapbox GL Cluster

Build Status
@urbica/react-map-gl-cluster.svg" alt="npm">
@urbica/react-map-gl-cluster.svg" alt="npm">

Cluster component for Urbica React Components Library for Mapbox GL JS.

Installation

  1. npm install mapbox-gl supercluster @urbica/react-map-gl @urbica/react-map-gl-cluster

…or if you are using yarn:

  1. yarn add mapbox-gl supercluster @urbica/react-map-gl @urbica/react-map-gl-cluster

Usage

  1. import { randomPoint } from '@turf/random';
  2. import MapGL, { Marker } from '@urbica/react-map-gl';
  3. import Cluster from '@urbica/react-map-gl-cluster';
  4. import 'mapbox-gl/dist/mapbox-gl.css';
  5. const bbox = [-160, -70, 160, 70];
  6. const points = randomPoint(50, { bbox }).features;
  7. points.forEach((point, index) => (point.id = index));
  8. initialState = {
  9. viewport: {
  10. latitude: 0,
  11. longitude: 0,
  12. zoom: 0
  13. }
  14. };
  15. const style = {
  16. width: '20px',
  17. height: '20px',
  18. color: '#fff',
  19. background: '#1978c8',
  20. borderRadius: '20px',
  21. textAlign: 'center'
  22. };
  23. const ClusterMarker = ({ longitude, latitude, pointCount }) => (
  24. <Marker longitude={longitude} latitude={latitude}>
  25. <div style={{ ...style, background: '#f28a25' }}>{pointCount}</div>
  26. </Marker>
  27. );
  28. <MapGL
  29. style={{ width: '100%', height: '400px' }}
  30. mapStyle='mapbox://styles/mapbox/light-v9'
  31. accessToken={MAPBOX_ACCESS_TOKEN}
  32. onViewportChange={viewport => setState({ viewport })}
  33. {...state.viewport}
  34. >
  35. <Cluster radius={40} extent={512} nodeSize={64} component={ClusterMarker}>
  36. {points.map(point => (
  37. <Marker
  38. key={point.id}
  39. longitude={point.geometry.coordinates[0]}
  40. latitude={point.geometry.coordinates[1]}
  41. >
  42. <div style={style} ></div>
  43. </Marker>
  44. ))}
  45. </Cluster>
  46. </MapGL>;