Skip to content

Commit

Permalink
feat: 聚合支持
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxingkang committed Jan 19, 2024
1 parent 219788e commit 6ec86b9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
26 changes: 21 additions & 5 deletions src/components/MarkerCluster/MarkerCluster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ const InternalMarkerCluster = <D extends AnyObject = AnyObject>(
props: MarkerClusterProps<D>,
ref: React.Ref<Supercluster>,
) => {
const { cluster, render, renderCluster, data = [], zoomOnClick, zoomOnClickPadding = 20 } = props;
const {
cluster,
render,
renderCluster,
data = [],
zoomOnClick,
zoomOnClickPadding = 20,
onClick,
onClusterClick,
} = props;
const { map } = useMap();
const [list, setList] = useState<any[]>([]);

Expand Down Expand Up @@ -43,7 +52,7 @@ const InternalMarkerCluster = <D extends AnyObject = AnyObject>(
}, [map]);

useEffect(() => {
if (data && data.length) {
if (data) {
supercluster.load(data);
handleChangeBoundary();
}
Expand Down Expand Up @@ -104,18 +113,25 @@ const InternalMarkerCluster = <D extends AnyObject = AnyObject>(
key={item.id}
lngLat={geometry.coordinates}
onClick={() => {
onClusterClick?.(point_count, cluster_id);
if (zoomOnClick) {
handleClusterMarkerClick(JSON.parse(JSON.stringify(item)));
handleClusterMarkerClick(item);
}
}}
>
{isFunction(renderCluster) ? renderCluster(point_count, cluster_id) : renderCluster}
{isFunction(renderCluster) ? renderCluster(point_count) : renderCluster}
</Marker>
);
}

return (
<Marker key={item.id} lngLat={geometry.coordinates}>
<Marker
key={item.id}
lngLat={geometry.coordinates}
onClick={() => {
onClick?.(properties);
}}
>
{isFunction(render) ? render(item) : render}
</Marker>
);
Expand Down
6 changes: 3 additions & 3 deletions src/components/MarkerCluster/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { PaddingOptions } from 'mapbox-gl';

export type AnyObject = Record<PropertyKey, any>;
export type RenderMarkerFun = (data: any) => React.ReactNode;
export type RenderClusterMarkerFun = (count: number, clusterId: string) => React.ReactNode;
export type RenderClusterMarkerFun = (count: number) => React.ReactNode;

export type { Supercluster };

Expand Down Expand Up @@ -49,8 +49,8 @@ export interface MarkerClusterProps<D extends object = any> {
* @default 20
*/
zoomOnClickPadding?: number | PaddingOptions;
onClick?: any;
onClusterClick?: any;
onClick?: (data: D) => void;
onClusterClick?: (count: number, clusterId: number) => void;
}

export type RefMarkerCluster = <D extends AnyObject = AnyObject>(
Expand Down
11 changes: 10 additions & 1 deletion stories/examples/ClusterPro.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const meta = {
>
<StyleLoadFinish>
<MarkerCluster
cluster={{ radius: 50 }}
ref={clusterRef}
data={features}
zoomOnClick
Expand All @@ -40,6 +39,16 @@ const meta = {
</Avatar>
);
}}
onClick={(data) => {
console.log(data);
}}
onClusterClick={(count, clusterId) => {
console.log(count);

if (clusterRef.current) {
console.log(clusterRef.current.getLeaves(clusterId, Infinity));
}
}}
/>
</StyleLoadFinish>
</Map>
Expand Down

0 comments on commit 6ec86b9

Please sign in to comment.