Skip to content

Commit

Permalink
feat(MarkerCluster): 支持 data
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxingkang committed Jan 19, 2024
1 parent e1cca4e commit 219788e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
16 changes: 7 additions & 9 deletions src/components/MarkerCluster/MarkerCluster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ const InternalMarkerCluster = <D extends AnyObject = AnyObject>(
return undefined;
}, [map]);

useImperativeHandle(ref, () => supercluster, []);

useEffect(() => {
fetch('https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson')
.then((res) => res.json())
.then((data) => {
supercluster.load(data.features);
handleChangeBoundary();
});
}, []);
if (data && data.length) {
supercluster.load(data);
handleChangeBoundary();
}
}, [data]);

useImperativeHandle(ref, () => supercluster, []);

const handleChangeBoundary = useCallback(
debounce(() => {
Expand Down
14 changes: 12 additions & 2 deletions stories/examples/ClusterPro.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import React, { useRef } from 'react';
import React, { useRef, useEffect, useState } from 'react';
import { Avatar } from 'antd';
import { Map, Marker, StyleLoadFinish, MarkerCluster } from '../../src';

import type { Meta, StoryObj } from '@storybook/react';
import type { Supercluster } from '../../src';
import type { Supercluster, MarkerClusterProps } from '../../src';

const meta = {
title: '示例/ClusterPro',
render: () => {
const [features, setFeatures] = useState<MarkerClusterProps['data']>([]);
const clusterRef = useRef<Supercluster>(null);

useEffect(() => {
fetch('https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson')
.then((res) => res.json())
.then((data) => {
setFeatures(data.features);
});
}, []);

return (
<Map
zoom={3}
Expand All @@ -21,6 +30,7 @@ const meta = {
<MarkerCluster
cluster={{ radius: 50 }}
ref={clusterRef}
data={features}
zoomOnClick
render={<Avatar style={{ backgroundColor: '#fde3cf', color: '#f56a00' }}>U</Avatar>}
renderCluster={(count) => {
Expand Down

0 comments on commit 219788e

Please sign in to comment.