mirror of https://gitee.com/antv-l7/antv-l7
feat: 增强 setData 的使用,使之有更高的自由度
This commit is contained in:
parent
e296c52587
commit
0dd2b14153
|
@ -50,7 +50,7 @@ import Source from '@antv/l7-source';
|
|||
import { encodePickingColor } from '@antv/l7-utils';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import { Container } from 'inversify';
|
||||
import { isFunction, isObject, isUndefined } from 'lodash';
|
||||
import { isFunction, isObject, isUndefined, isEqual } from 'lodash';
|
||||
import { BlendTypes } from '../utils/blend';
|
||||
import { handleStyleDataMapping } from '../utils/dataMappingStyle';
|
||||
import {
|
||||
|
@ -521,11 +521,26 @@ export default class BaseLayer<ChildLayerStyleOptions = {}> extends EventEmitter
|
|||
return this;
|
||||
}
|
||||
public setData(data: any, options?: ISourceCFG) {
|
||||
const currentSource = this.getSource()
|
||||
if (this.inited) {
|
||||
this.layerSource.setData(data, options);
|
||||
if(currentSource && !isEqual(currentSource.parser, options)) {
|
||||
// 在 source 不一致的情况下,需要重新设置 source (parser/data 格式解析规则需要重新生成)
|
||||
this.source(new Source(data, options));
|
||||
this.sourceEvent();
|
||||
} else {
|
||||
this.layerSource.setData(data, options);
|
||||
}
|
||||
// this.layerSource.setData(data, options);
|
||||
} else {
|
||||
this.on('inited', () => {
|
||||
this.layerSource.setData(data, options);
|
||||
if(!currentSource) {
|
||||
// 执行 setData 的时候 source 还不存在(还未执行 addLayer)
|
||||
this.source(new Source(data, options));
|
||||
this.sourceEvent();
|
||||
} else {
|
||||
this.layerSource.setData(data, options);
|
||||
}
|
||||
// this.layerSource.setData(data, options);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -49,21 +49,12 @@ export default function json(data: IJsonData, cfg: IParserCfg): IParserData {
|
|||
}
|
||||
|
||||
// TODO: 提供默认数据和解析器
|
||||
export const defaultData = [
|
||||
{
|
||||
lng1: 100,
|
||||
lat1: 30.0,
|
||||
lng2: 130,
|
||||
lat2: 30,
|
||||
},
|
||||
];
|
||||
export const defaultData = [];
|
||||
|
||||
export const defaultParser = {
|
||||
parser: {
|
||||
type: 'json',
|
||||
x: 'lng1',
|
||||
y: 'lat1',
|
||||
x1: 'lng2',
|
||||
y1: 'lat2',
|
||||
x: 'lng',
|
||||
y: 'lat'
|
||||
},
|
||||
};
|
||||
|
|
|
@ -19,7 +19,6 @@ export default class Amap2demo extends React.Component {
|
|||
}
|
||||
|
||||
public async componentDidMount() {
|
||||
|
||||
const scene = new Scene({
|
||||
id: 'map',
|
||||
map: new GaodeMap({
|
||||
|
@ -31,59 +30,55 @@ export default class Amap2demo extends React.Component {
|
|||
zoom: 4,
|
||||
}),
|
||||
});
|
||||
scene.addImage(
|
||||
'00',
|
||||
'https://gw.alipayobjects.com/zos/basement_prod/604b5e7f-309e-40db-b95b-4fac746c5153.svg',
|
||||
);
|
||||
|
||||
const data = {
|
||||
"type": "FeatureCollection",
|
||||
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
|
||||
"features": [
|
||||
{ "type": "Feature", "properties":
|
||||
{ "id": "ak16994521", "mag": 2.3, "time": 1507425650893, "felt": null, "tsunami": 0 },
|
||||
"geometry":
|
||||
{
|
||||
"type": "Point",
|
||||
"coordinates": [ 120, 30, 0.0 ]
|
||||
}
|
||||
}]
|
||||
}
|
||||
const layer = new PointLayer()
|
||||
// .source(data)
|
||||
.source({
|
||||
"type": "FeatureCollection",
|
||||
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
|
||||
"features": [
|
||||
{ "type": "Feature", "properties":
|
||||
{ "id": "ak16994521", "mag": 2.3, "time": 1507425650893, "felt": null, "tsunami": 0 },
|
||||
"geometry":
|
||||
{
|
||||
"type": "Point",
|
||||
"coordinates": [ 125, 30, 0.0 ]
|
||||
}
|
||||
}]
|
||||
})
|
||||
.shape('circle')
|
||||
.size(40)
|
||||
.color('#000')
|
||||
|
||||
|
||||
scene.on('loaded', () => {
|
||||
|
||||
// const layer = new PointLayer().source([
|
||||
// { lng: 120, lat: 30, name: '00' }
|
||||
// ], {
|
||||
// parser: {
|
||||
// type: 'json',
|
||||
// x: 'lng',
|
||||
// y: 'lat'
|
||||
// }
|
||||
// })
|
||||
// .shape('name', ['00'])
|
||||
// .size(20)
|
||||
|
||||
// scene.addLayer(layer);
|
||||
|
||||
setTimeout(() => {
|
||||
scene.setZoom(4)
|
||||
// scene.setZoomAndCenter(4, [120, 30])
|
||||
console.log('rezoom')
|
||||
}, 2000)
|
||||
|
||||
const lineLayer = new LineLayer()
|
||||
.source(
|
||||
[
|
||||
{
|
||||
lng1: 120,
|
||||
lat1: 30,
|
||||
lng2: 122,
|
||||
lat2: 30,
|
||||
},
|
||||
],
|
||||
{
|
||||
parser: {
|
||||
type: 'json',
|
||||
x: 'lng1',
|
||||
y: 'lat1',
|
||||
x1: 'lng2',
|
||||
y1: 'lat2',
|
||||
},
|
||||
},
|
||||
)
|
||||
.shape('line')
|
||||
.size(2)
|
||||
.color('#f00');
|
||||
|
||||
scene.addLayer(lineLayer);
|
||||
|
||||
|
||||
layer.setData([{
|
||||
lng: 120, lat: 30
|
||||
}], {
|
||||
parser: {
|
||||
type: 'json',
|
||||
x: 'lng',
|
||||
y: 'lat'
|
||||
}
|
||||
})
|
||||
// layer.setData(data)
|
||||
|
||||
scene.addLayer(layer);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue