Skip to content

Commit

Permalink
【feature】mapboxgl querySourceFeatures 统一返回的结构 geoJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongjiaojiao committed Jul 16, 2024
1 parent 86feffa commit d3eca96
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
29 changes: 17 additions & 12 deletions src/mapboxgl/overlay/L7Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,23 +309,28 @@ export class L7Layer extends CustomOverlayLayer {
const _this = this;
this.l7layer.boxSelect([Math.min(x1, x2), Math.min(y1, y2), Math.max(x1, x2), Math.max(y1, y2)], (features) => {
const nextFeatures = features || [];
cb(
nextFeatures.map((item) => {
return {
...item,
layer: _this.getLayer()
};
})
);
const { layerCapture = true } = options || {};
if (layerCapture) {
cb(
nextFeatures.map((item) => {
return {
...item,
layer: _this.getLayer()
};
})
);
return;
}
cb(nextFeatures);
});
}

querySourceFeatures() {
if (!this.l7layer || !this.l7layer.rawConfig.visible) {
return [];
}
const layerSource = this.l7layer.layerSource;
let datas = layerSource.data.dataArray;
const { layerSource, pickingService } = this.l7layer;
let datas = pickingService.handleRawFeature(layerSource.data.dataArray);
const { parser: { type } } = layerSource;
if (type === 'mvt') {
const { tileset: { cacheTiles = [] } = {} } = layerSource;
Expand All @@ -339,7 +344,7 @@ export class L7Layer extends CustomOverlayLayer {
(!(item.properties || {})[featureId] ||
!mvtDatas.some((feature) => feature.properties[featureId] === item.properties[featureId]))
);
mvtDatas.push(...features);
mvtDatas.push(...pickingService.handleRawFeature(features));
});
datas = datas.length > mvtDatas.length ? datas : mvtDatas;
}
Expand All @@ -351,7 +356,7 @@ export class L7Layer extends CustomOverlayLayer {
[0, 0],
[this.map.transform.width - 1, this.map.transform.height - 1] // -1 是解决报错问题
];
this.queryRenderedFeatures(bounds, undefined, cb);
this.queryRenderedFeatures(bounds, { layerCapture: false }, cb);
}
return datas;
}
Expand Down
9 changes: 8 additions & 1 deletion test/mapboxgl/overlay/L7LayerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,14 @@ describe('mapboxgl L7Layer', () => {
expect(queryResult.cb.calls.count()).toBe(1);
expect(queryFeatures).not.toBeUndefined();
expect(queryFeatures.length).toBeGreaterThan(0);
expect(layer.querySourceFeatures().length).toBeGreaterThan(0);
expect(queryFeatures[0].geometry).not.toBeUndefined();
expect(queryFeatures[0].properties).not.toBeUndefined();
expect(queryFeatures[0].layer).not.toBeUndefined();
const sourceFeatures = layer.querySourceFeatures();
expect(sourceFeatures.length).toBeGreaterThan(0);
expect(sourceFeatures[0].geometry).not.toBeUndefined();
expect(sourceFeatures[0].properties).not.toBeUndefined();
expect(sourceFeatures[0].layer).toBeUndefined();
expect(layer.getLayer().layout.visibility).toBe('visible');
layer.setLayoutProperty('visibility', 'none');
expect(layer.getLayer().layout.visibility).toBe('none');
Expand Down
42 changes: 42 additions & 0 deletions test/tool/mock_l7.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,54 @@ class Layer {
dataArray: []
}
};
this.pickingService = {
handleRawFeature: function (rawFeature) {
rawFeature = rawFeature instanceof Array ? rawFeature : [rawFeature];
const res = rawFeature.map((item) => {
if (item === 'null') {
return item;
}
if (item.type === 'Feature') {
return item;
}
const newFeature = {
properties: {},
geometry: { type: '', coordinates: [] },
};
const coordinates = item.coordinates;
delete item.coordinates;
newFeature.properties = item;
if (coordinates) {
newFeature.geometry = { type: '', coordinates };
}
return newFeature;
});
return res;
}
}
if (this.rawConfig.visible === void 0) {
this.rawConfig.visible = true;
}
}
source(data, options = {}) {
const parser = options.parser || { type: "geojson" };
let dataArray = [];
if (parser.type === "geojson") {
dataArray = data;
}
if (parser.type === 'json') {
dataArray = [
{
航班有效期结束: 2016.11,
到达城市: '北京',
smpid: 1,
coordinates: [
[80.30091874, 41.26940127],
[116.395645, 39.92998578]
]
}
];
}
this.layerSource = {
...options,
parser,
Expand Down

0 comments on commit d3eca96

Please sign in to comment.