fix(layer): 解决PointLayer中配置了cluster:true缺少部分数据的问题

fix #508
This commit is contained in:
wangbin 2020-09-07 16:50:56 +08:00
parent 6ff115ddb5
commit 710679041c
6 changed files with 21 additions and 7 deletions

View File

@ -62,7 +62,7 @@ export interface ISource {
cluster: boolean;
clusterOptions: Partial<IClusterOptions>;
setData(data: any): void;
updateClusterData(zoom: number): void;
updateClusterData(bbox: [[number, number], [number, number]], zoom: number): void;
getFeatureById(id: number): unknown;
getFeatureId(field: string, value: any): number | undefined;
getClusters(zoom: number): any;

View File

@ -746,8 +746,9 @@ export default class BaseLayer<ChildLayerStyleOptions = {}> extends EventEmitter
public setSource(source: Source) {
this.layerSource = source;
const zoom = this.mapService.getZoom();
const viewBounds = this.mapService.getBounds();
if (this.layerSource.cluster) {
this.layerSource.updateClusterData(zoom);
this.layerSource.updateClusterData(viewBounds, zoom);
}
// source 可能会复用会在其它layer被修改
this.layerSource.on('update', this.sourceEvent);

View File

@ -27,8 +27,9 @@ export default class DataSourcePlugin implements ILayerPlugin {
const cluster = source.cluster;
const { zoom = 0, maxZoom = 16 } = source.clusterOptions;
const newZoom = this.mapService.getZoom() - 1;
const viewBounds = this.mapService.getBounds();
if (cluster && Math.abs(zoom - newZoom) > 1 && maxZoom > zoom) {
source.updateClusterData(Math.floor(newZoom));
source.updateClusterData(viewBounds, Math.floor(newZoom));
return true;
}
return false;

View File

@ -1,6 +1,7 @@
import Source from '../src/source';
import Point from './data/point';
import Polygon from './data/polygon';
import { bBoxToBounds } from '@antv/l7-utils';
describe('source constructor', () => {
it('source.constructor', () => {
@ -20,6 +21,6 @@ describe('source constructor', () => {
field: 'mag',
},
});
source.updateClusterData(2);
source.updateClusterData(bBoxToBounds(source.extent), 2);
});
});

View File

@ -11,7 +11,7 @@ import {
lazyInject,
TYPES,
} from '@antv/l7-core';
import { extent } from '@antv/l7-utils';
import { extent, boundsContains, bBoxToBounds } from '@antv/l7-utils';
import {
BBox,
Feature,
@ -85,9 +85,12 @@ export default class Source extends EventEmitter {
public getClustersLeaves(id: number): any {
return this.clusterIndex.getLeaves(id, Infinity);
}
public updateClusterData(zoom: number): void {
public updateClusterData(bbox: [[number,number],[number,number]],zoom: number): void {
const { method = 'sum', field } = this.clusterOptions;
let data = this.clusterIndex.getClusters(this.extent, Math.floor(zoom));
if (!boundsContains(bbox, bBoxToBounds(this.extent))) {
return;
}
let data = this.clusterIndex.getClusters(bbox[0].concat(bbox[1]), Math.floor(zoom));
this.clusterOptions.zoom = zoom;
data.forEach((p: any) => {
if (!p.id) {

View File

@ -239,3 +239,11 @@ export function boundsContains(b1: IBounds, b2: IBounds): boolean {
b1[1][1] >= b2[1][1]
);
}
/**
* bbox Bounds
* @param b1 bbox
*
*/
export function bBoxToBounds(b1: BBox): IBounds {
return [[b1[0], b1[1]], [b1[2], b1[3]]];
}