Skip to content

Commit

Permalink
fix: 修复地图状态更新 (#8)
Browse files Browse the repository at this point in the history
* fix: 修复地图状态更新

* chore: 深度对比使用 ahooks

* fix: 修复单独实例情况下 mapOptions 没有默认值

* fix: 修复图层摧毁时

* chore: 优化图层组件重复渲染

* chore: update to 0.0.1-alpha.3
  • Loading branch information
lvisei authored Jun 8, 2022
1 parent e653c35 commit e64644d
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 93 deletions.
1 change: 1 addition & 0 deletions .umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default defineConfig({
hash: true,
// 同步 gh-page CNAME 文件
copy: isProduction ? ['docs/CNAME'] : [],
devtool: isProduction ? false : 'eval',
externals: {
react: 'window.React',
'react-dom': 'window.ReactDOM',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/larkmap",
"version": "0.0.1-alpha.2",
"version": "0.0.1-alpha.3",
"description": "A React toolkit for geospatial visualization based on L7",
"license": "MIT",
"main": "lib/index.js",
Expand Down Expand Up @@ -32,7 +32,7 @@
"version": "node scripts/sync-version"
},
"dependencies": {
"@antv/l7-composite-layers": "^0.0.1-alpha.5",
"@antv/l7-composite-layers": "^0.0.1-alpha.6",
"ahooks": "^3.3.13",
"classnames": "^2.3.1"
},
Expand Down
36 changes: 15 additions & 21 deletions src/components/LarkMap/hooks/use-layer/demos/default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ const source = {
],
parser: { type: 'json', x: 'lng', y: 'lat' },
};
const state = {
active: {
strokeColor: 'red',
lineWidth: 2,
lineOpacity: 1,
const layerOptions = {
autoFit: false,
radius: 40,
fillColor: '#0f9960',
opacity: 0.4,
strokeColor: 'blue',
lineWidth: 2,
state: {
active: { strokeColor: 'red', lineWidth: 2, lineOpacity: 1 },
},
label: {
field: 't',
visible: true,
style: { fill: '#454d64', fontSize: 18, stroke: '#fff', strokeWidth: 2, textOffset: [0, -20] as [number, number] },
},
};

Expand All @@ -29,22 +38,7 @@ const MyComponent = () => {
export default () => {
return (
<LarkMap mapType="GaodeV1" style={{ height: '300px' }}>
<BubbleLayer
id="myBubbleLayer"
source={source}
autoFit={false}
radius={40}
fillColor="#0f9960"
opacity={0.4}
strokeColor="blue"
lineWidth={2}
state={state}
label={{
field: 't',
visible: true,
style: { fill: '#454d64', fontSize: 18, stroke: '#fff', strokeWidth: 2, textOffset: [0, -20] },
}}
/>
<BubbleLayer {...layerOptions} id="myBubbleLayer" source={source} />
<MyComponent />
</LarkMap>
);
Expand Down
42 changes: 42 additions & 0 deletions src/components/LarkMap/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Scene } from '@antv/l7';
import { useDeepCompareEffect } from 'ahooks';
import classNames from 'classnames';
import type { CSSProperties } from 'react';
import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
Expand Down Expand Up @@ -57,10 +58,50 @@ export const LarkMap = forwardRef<LarkMapRefAttributes, LarkMapProps>((props, re
scene.destroy();
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useImperativeHandle(ref, () => ({ getScene: () => sceneInstance, getMap: () => sceneInstance.map }), [sceneInstance]);

// 更新地图样式
useEffect(() => {
if (sceneInstance && mapOptions.style) {
sceneInstance.setMapStyle(mapOptions.style);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mapOptions.style]);

// 更新地图层级
useEffect(() => {
if (sceneInstance && mapOptions.zoom) {
sceneInstance.setZoom(mapOptions.zoom);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mapOptions.zoom]);

// 更新地图视野中心点
useDeepCompareEffect(() => {
if (sceneInstance && mapOptions.center) {
sceneInstance.setCenter(mapOptions.center);
}
}, [mapOptions.center]);

// 更新地图视野倾角
useEffect(() => {
if (sceneInstance && mapOptions.pitch) {
sceneInstance.setPitch(mapOptions.pitch);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mapOptions.pitch]);

// 更新地图旋转角度
useEffect(() => {
if (sceneInstance && mapOptions.rotation) {
sceneInstance.setRotation(mapOptions.rotation);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mapOptions.rotation]);

const styles: CSSProperties = useMemo(
() => ({
position: 'relative',
Expand All @@ -80,4 +121,5 @@ export const LarkMap = forwardRef<LarkMapRefAttributes, LarkMapProps>((props, re

LarkMap.defaultProps = {
mapType: 'Mapbox',
mapOptions: {},
};
35 changes: 15 additions & 20 deletions src/components/Layers/BubbleLayer/demos/default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,27 @@ const source = {
],
parser: { type: 'json', x: 'lng', y: 'lat' },
};
const state = {
active: {
strokeColor: 'red',
lineWidth: 2,
lineOpacity: 1,
const layerOptions = {
autoFit: true,
radius: 40,
fillColor: '#0f9960',
opacity: 0.4,
strokeColor: 'blue',
lineWidth: 2,
state: {
active: { strokeColor: 'red', lineWidth: 2, lineOpacity: 1 },
},
label: {
field: 't',
visible: true,
style: { fill: '#454d64', fontSize: 18, stroke: '#fff', strokeWidth: 2, textOffset: [0, -20] as [number, number] },
},
};

export default () => {
return (
<LarkMap mapType="GaodeV1" style={{ height: '300px' }}>
<BubbleLayer
source={source}
autoFit={true}
radius={40}
fillColor="#0f9960"
opacity={0.4}
strokeColor="blue"
lineWidth={2}
state={state}
label={{
field: 't',
visible: true,
style: { fill: '#454d64', fontSize: 18, stroke: '#fff', strokeWidth: 2, textOffset: [0, -20] },
}}
/>
<BubbleLayer {...layerOptions} source={source} />
</LarkMap>
);
};
14 changes: 8 additions & 6 deletions src/components/Layers/BubbleLayer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { BubbleLayer as L7BubbleLayer } from '@antv/l7-composite-layers';
import { forwardRef, useImperativeHandle } from 'react';
import { forwardRef, memo, useImperativeHandle } from 'react';
import { useCreateLayer } from '../hooks/use-create-layer';
import type { BubbleLayerProps } from './types';

export type { BubbleLayerProps };

export const BubbleLayer = forwardRef<L7BubbleLayer, BubbleLayerProps>((props, ref) => {
const layerRef = useCreateLayer<L7BubbleLayer, BubbleLayerProps>(L7BubbleLayer, props);
export const BubbleLayer = memo(
forwardRef<L7BubbleLayer, BubbleLayerProps>((props, ref) => {
const layerRef = useCreateLayer<L7BubbleLayer, BubbleLayerProps>(L7BubbleLayer, props);

useImperativeHandle(ref, () => layerRef.current);
useImperativeHandle(ref, () => layerRef.current);

return null;
});
return null;
}),
);
33 changes: 10 additions & 23 deletions src/components/Layers/ChoroplethLayer/demos/default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,22 @@ const source = {
data: hangezhouGeoJSON,
parser: { type: 'geojson' },
};
const state = {
active: {
strokeColor: 'green',
lineWidth: 1.5,
lineOpacity: 0.8,
},
select: {
strokeColor: 'yellow',
lineWidth: 1.5,
lineOpacity: 0.8,
const layerOptions = {
autoFit: true,
fillColor: 'rgb(239,243,255)',
opacity: 0.3,
strokeColor: 'blue',
state: {
active: { strokeColor: 'green', lineWidth: 1.5, lineOpacity: 0.8 },
select: { strokeColor: 'yellow', lineWidth: 1.5, lineOpacity: 0.8 },
},
label: { field: 'name', visible: true, style: { fill: 'blue', fontSize: 12, stroke: '#fff', strokeWidth: 2 } },
};

export default () => {
return (
<LarkMap mapType="GaodeV1" style={{ height: '300px' }}>
<ChoroplethLayer
source={source}
autoFit={true}
fillColor="rgb(239,243,255)"
opacity={0.3}
strokeColor="blue"
state={state}
label={{
field: 'name',
visible: true,
style: { fill: 'blue', fontSize: 12, stroke: '#fff', strokeWidth: 2 },
}}
/>
<ChoroplethLayer {...layerOptions} source={source} />
</LarkMap>
);
};
14 changes: 8 additions & 6 deletions src/components/Layers/ChoroplethLayer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { ChoroplethLayer as L7ChoroplethLayer } from '@antv/l7-composite-layers';
import { forwardRef, useImperativeHandle } from 'react';
import { forwardRef, memo, useImperativeHandle } from 'react';
import { useCreateLayer } from '../hooks/use-create-layer';
import type { ChoroplethLayerProps } from './types';

export type { ChoroplethLayerProps };

export const ChoroplethLayer = forwardRef<L7ChoroplethLayer, ChoroplethLayerProps>((props, ref) => {
const layerRef = useCreateLayer<L7ChoroplethLayer, ChoroplethLayerProps>(L7ChoroplethLayer, props);
export const ChoroplethLayer = memo(
forwardRef<L7ChoroplethLayer, ChoroplethLayerProps>((props, ref) => {
const layerRef = useCreateLayer<L7ChoroplethLayer, ChoroplethLayerProps>(L7ChoroplethLayer, props);

useImperativeHandle(ref, () => layerRef.current);
useImperativeHandle(ref, () => layerRef.current);

return null;
});
return null;
}),
);
30 changes: 16 additions & 14 deletions src/components/Layers/hooks/use-create-layer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useUpdateEffect } from 'ahooks';
import { useRef } from 'react';
import { useEffect, useRef } from 'react';
import type { Layer } from 'utils/layer-manager';
import type { LayerCommonProps } from '../../../types/common';
import { useLayerManager } from '../../LarkMap/hooks';
Expand All @@ -22,8 +22,7 @@ export const useCreateLayer = <L extends Layer, C extends LayerCommonProps<L> &
) => {
const layerManager = useLayerManager();
const layerRef = useRef<L>();

const { onCreated } = config;
const { onCreated, source, ...options } = config;

// 生成图层
// 添加到 layerManager 自动加载到 scene
Expand All @@ -44,27 +43,30 @@ export const useCreateLayer = <L extends Layer, C extends LayerCommonProps<L> &
}
}

// config 更新时
// options 更新时
useUpdateEffect(() => {
if (layerRef.current) {
layerRef.current.update(options);
}
}, [options]);

// source 更新时
useUpdateEffect(() => {
if (layerRef.current) {
layerRef.current.update(config);
layerRef.current.changeData(source);
console.log('changeData: ');
}
}, [source]);

// 组件销毁时
// 组件销毁时
useEffect(() => {
return () => {
if (layerRef.current) {
layerManager.removeLayer(layerRef.current);
layerRef.current = null;
}
};
}, [config]);

// source 更新时
useUpdateEffect(() => {
if (layerRef.current) {
layerRef.current.changeData(config.source);
}
}, [config.source]);
}, []);

return layerRef;
};
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '0.0.1-alpha.2';
export default '0.0.1-alpha.3';

0 comments on commit e64644d

Please sign in to comment.