Skip to content

Commit

Permalink
feat(MarkerCluster): 代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxingkang committed Jan 19, 2024
1 parent dd18c92 commit 6df62fe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"dependencies": {
"@pansy/react-hooks": "^2.3.0",
"@pansy/shared": "^1.15.0",
"@turf/bbox": "^6.5.0",
"@turf/helpers": "^6.5.0",
"@types/supercluster": "^7.1.3",
"lodash-es": "^4.17.21",
"supercluster": "^8.0.0",
Expand Down
23 changes: 23 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/components/MarkerCluster/MarkerCluster.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { useState, useMemo, useEffect, useCallback, useImperativeHandle, forwardRef } from 'react';
import { isFunction } from '@pansy/shared';
import { LngLatBounds } from 'mapbox-gl';
import Supercluster from 'supercluster';
import { debounce } from 'lodash-es';
import bbox from '@turf/bbox';
import { featureCollection } from '@turf/helpers';
import { defaultSuperclusterOptions } from './config';
import { Marker } from '../Marker';
import { useMap } from '../../hooks/useMap';
Expand Down Expand Up @@ -51,6 +54,8 @@ export const MarkerCluster = forwardRef<Supercluster, MarkerClusterProps>((props
debounce(() => {
const mapBoundary = getMapBoundary();

console.log(333);

const result = supercluster.getClusters(...mapBoundary);

setList(result);
Expand All @@ -73,14 +78,27 @@ export const MarkerCluster = forwardRef<Supercluster, MarkerClusterProps>((props
const { properties } = data;

if (!properties?.cluster) return;

console.log(111);
const children = supercluster.getLeaves(properties.cluster_id, Infinity);
console.log(222);
const childrenBbox = bbox(featureCollection(children));

map.fitBounds(LngLatBounds.convert(childrenBbox as any), {
padding: 20,
});
};

console.log(list);

return (
<>
{list.map((item, index) => {
const { geometry, properties } = item;
const { point_count, cluster, cluster_id } = properties;

console.log(item);

if (cluster) {
return (
<Marker
Expand Down

0 comments on commit 6df62fe

Please sign in to comment.