mirror of https://gitee.com/antv-l7/antv-l7
feat(l7-layers): reuse source for multi layer
This commit is contained in:
parent
b08b904904
commit
9a6450a9a9
|
@ -769,9 +769,16 @@ export default class BaseLayer<ChildLayerStyleOptions = {}> extends EventEmitter
|
||||||
}
|
}
|
||||||
|
|
||||||
public setSource(source: Source) {
|
public setSource(source: Source) {
|
||||||
|
// 清除旧 sources 事件
|
||||||
|
if (this.layerSource) {
|
||||||
|
this.layerSource.off('update', this.sourceEvent);
|
||||||
|
}
|
||||||
|
|
||||||
this.layerSource = source;
|
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);
|
this.layerSource.updateClusterData(zoom);
|
||||||
}
|
}
|
||||||
// source 可能会复用,会在其它layer被修改
|
// source 可能会复用,会在其它layer被修改
|
||||||
|
|
|
@ -15,8 +15,12 @@ export default class DataSourcePlugin implements ILayerPlugin {
|
||||||
public apply(layer: ILayer) {
|
public apply(layer: ILayer) {
|
||||||
this.mapService = layer.getContainer().get<IMapService>(TYPES.IMapService);
|
this.mapService = layer.getContainer().get<IMapService>(TYPES.IMapService);
|
||||||
layer.hooks.init.tap('DataSourcePlugin', () => {
|
layer.hooks.init.tap('DataSourcePlugin', () => {
|
||||||
const { data, options } = layer.sourceOption;
|
const source = layer.getSource();
|
||||||
layer.setSource(new Source(data, options));
|
if (!source) {
|
||||||
|
const { data, options } = layer.sourceOption;
|
||||||
|
layer.setSource(new Source(data, options));
|
||||||
|
}
|
||||||
|
|
||||||
this.updateClusterData(layer);
|
this.updateClusterData(layer);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import { storiesOf } from '@storybook/react';
|
import { storiesOf } from '@storybook/react';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import HolePolygon from './components/hole'
|
import HolePolygon from './components/hole';
|
||||||
import Line from './components/line';
|
import Line from './components/line';
|
||||||
import MultiLine from './components/multiLine';
|
import MultiLine from './components/multiLine';
|
||||||
import MultiPolygon from './components/multiPolygon';
|
import MultiPolygon from './components/multiPolygon';
|
||||||
import UpdatePolygon from './components/updatedata';
|
import UpdatePolygon from './components/ReuseSource';
|
||||||
|
import ReuseSource from './components/ReuseSource';
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import notes from './Map.md';
|
import notes from './Map.md';
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
@ -13,4 +14,5 @@ storiesOf('数据', module)
|
||||||
.add('updatePolygon', () => <UpdatePolygon />, {})
|
.add('updatePolygon', () => <UpdatePolygon />, {})
|
||||||
.add('MultiLine', () => <MultiLine />, {})
|
.add('MultiLine', () => <MultiLine />, {})
|
||||||
.add('HolePolygon', () => <HolePolygon />, {})
|
.add('HolePolygon', () => <HolePolygon />, {})
|
||||||
.add('折线', () => <Line />, {});
|
.add('折线', () => <Line />, {})
|
||||||
|
.add('复用 Source', () => <ReuseSource />, {});
|
||||||
|
|
|
@ -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,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue