feat(l7-layers): reuse source for multi layer

This commit is contained in:
yunji 2021-06-18 11:29:50 +08:00
parent b08b904904
commit 9a6450a9a9
4 changed files with 91 additions and 7 deletions

View File

@ -769,9 +769,16 @@ export default class BaseLayer<ChildLayerStyleOptions = {}> extends EventEmitter
}
public setSource(source: Source) {
// 清除旧 sources 事件
if (this.layerSource) {
this.layerSource.off('update', this.sourceEvent);
}
this.layerSource = source;
const zoom = this.mapService.getZoom();
if (this.layerSource.cluster) {
// 已 inited 且启用聚合进行更新聚合数据
if (this.inited && this.layerSource.cluster) {
const zoom = this.mapService.getZoom();
this.layerSource.updateClusterData(zoom);
}
// source 可能会复用会在其它layer被修改

View File

@ -15,8 +15,12 @@ export default class DataSourcePlugin implements ILayerPlugin {
public apply(layer: ILayer) {
this.mapService = layer.getContainer().get<IMapService>(TYPES.IMapService);
layer.hooks.init.tap('DataSourcePlugin', () => {
const { data, options } = layer.sourceOption;
layer.setSource(new Source(data, options));
const source = layer.getSource();
if (!source) {
const { data, options } = layer.sourceOption;
layer.setSource(new Source(data, options));
}
this.updateClusterData(layer);
});

View File

@ -1,10 +1,11 @@
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import HolePolygon from './components/hole'
import HolePolygon from './components/hole';
import Line from './components/line';
import MultiLine from './components/multiLine';
import MultiPolygon from './components/multiPolygon';
import UpdatePolygon from './components/updatedata';
import UpdatePolygon from './components/ReuseSource';
import ReuseSource from './components/ReuseSource';
// @ts-ignore
import notes from './Map.md';
// @ts-ignore
@ -13,4 +14,5 @@ storiesOf('数据', module)
.add('updatePolygon', () => <UpdatePolygon />, {})
.add('MultiLine', () => <MultiLine />, {})
.add('HolePolygon', () => <HolePolygon />, {})
.add('折线', () => <Line />, {});
.add('折线', () => <Line />, {})
.add('复用 Source', () => <ReuseSource />, {});

View File

@ -0,0 +1,71 @@
import { LineLayer, PolygonLayer, Scene } from '@antv/l7';
import { GaodeMap } from '@antv/l7-maps';
import Source from '@antv/l7-source';
import * as React from 'react';
export default class ReuseSource extends React.Component {
private scene: Scene;
public componentWillUnmount() {
this.scene.destroy();
}
public async componentDidMount() {
const scene = new Scene({
id: 'map',
map: new GaodeMap({
style: 'dark',
center: [104.288144, 31.239692],
zoom: 4.4,
}),
});
scene.on('loaded', async () => {
const response = await fetch(
'https://gw.alipayobjects.com/os/rmsportal/JToMOWvicvJOISZFCkEI.json',
);
const data = await response.json();
const source = new Source(data);
const polygonLayer = new PolygonLayer({})
.shape('fill')
.color('rgba(255,255,255,0.2)')
.select({
color: 'red',
})
.style({
opacity: 1,
});
polygonLayer.setSource(source);
polygonLayer.on('click', (e) => {
source.setData({
type: 'FeatureCollection',
features: [e.feature],
});
});
const lineLayer = new LineLayer({}).shape('line').color('#0ffbc4');
lineLayer.setSource(source);
scene.addLayer(polygonLayer);
scene.addLayer(lineLayer);
this.scene = scene;
});
}
public render() {
return (
<div
id="map"
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
/>
);
}
}