diff --git a/packages/core/src/services/interaction/IPickingService.ts b/packages/core/src/services/interaction/IPickingService.ts index 7b230d0cbf..84c7b03beb 100644 --- a/packages/core/src/services/interaction/IPickingService.ts +++ b/packages/core/src/services/interaction/IPickingService.ts @@ -56,5 +56,5 @@ export interface ILayerPickService { * 获取选中的要素 * @param id q */ - getFeatureById(id: number, lngLat?: ILngLat): any; + getFeatureById(id: number, lngLat?: ILngLat, featureId): any; } diff --git a/packages/core/src/services/interaction/PickingService.ts b/packages/core/src/services/interaction/PickingService.ts index d1af0e8988..4bab1ffd8b 100644 --- a/packages/core/src/services/interaction/PickingService.ts +++ b/packages/core/src/services/interaction/PickingService.ts @@ -134,17 +134,15 @@ export default class PickingService implements IPickingService { const features = []; const featuresIdMap: { [key: string]: boolean } = {}; + const { featureId } = layer.getLayerConfig(); for (let i = 0; i < pickedColors.length / 4; i = i + 1) { const color = pickedColors.slice(i * 4, i * 4 + 4); const pickedFeatureIdx = decodePickingColor(color); if (pickedFeatureIdx !== -1 && !featuresIdMap[pickedFeatureIdx]) { - const rawFeature = - layer.layerPickService.getFeatureById(pickedFeatureIdx); - features.push({ - // @ts-ignore - ...rawFeature, - pickedFeatureIdx, - }); + let rawFeature = + layer.layerPickService.getFeatureById(pickedFeatureIdx, featureId); + rawFeature = rawFeature instanceof Array ? rawFeature : [rawFeature]; + features.push(...rawFeature); featuresIdMap[pickedFeatureIdx] = true; } } diff --git a/packages/layers/src/tile/service/TilePickService.ts b/packages/layers/src/tile/service/TilePickService.ts index 499ee2db0d..1742b9e4f1 100644 --- a/packages/layers/src/tile/service/TilePickService.ts +++ b/packages/layers/src/tile/service/TilePickService.ts @@ -48,7 +48,6 @@ export class TilePickService implements ITilePickService { const [x, y, x1, y1]= box; const minLngLat = map.unproject({x, y}); const maxLngLat = map.unproject({x:x1, y:y1}) - console.log(minLngLat, maxLngLat) const tiles = this.tileLayerService.getVisibleTileByBox(minLngLat, maxLngLat); tiles.forEach(tile=>{ // TODO 多图层拾取 @@ -134,7 +133,7 @@ export class TilePickService implements ITilePickService { } /** 从瓦片中根据数据 */ - public getFeatureById(pickedFeatureIdx: number) { + public getFeatureById(pickedFeatureIdx: number, featureId?: string) { // 提取当前可见瓦片 const tiles = this.tileLayerService .getTiles() @@ -142,7 +141,7 @@ export class TilePickService implements ITilePickService { // 提取当前可见瓦片中匹配 ID 的 feature 列表 const features: any[] = []; tiles.forEach((tile: ITile) => { - features.push(...tile.getFeatureById(pickedFeatureIdx)); + features.push(...tile.getFeatureById(pickedFeatureIdx, featureId)); }); // 将 feature 列表合并后返回 diff --git a/packages/layers/src/tile/tileFactory/VectorTile.ts b/packages/layers/src/tile/tileFactory/VectorTile.ts index 53b61e1ff1..812da256da 100644 --- a/packages/layers/src/tile/tileFactory/VectorTile.ts +++ b/packages/layers/src/tile/tileFactory/VectorTile.ts @@ -1,5 +1,6 @@ import { ILayer, ILayerAttributesOption } from '@antv/l7-core'; import { VectorSource } from '@antv/l7-source'; +import { cloneDeep } from 'lodash'; import Tile from './Tile'; import { getTileLayer } from './util'; @@ -79,12 +80,29 @@ export default class VectorTile extends Tile { * @param id * @returns */ - public getFeatureById(id: number) { + public getFeatureById(_id: number, featureId = 'id') { const layer = this.getMainLayer(); if (!layer) { return []; } - const res = layer.getSource().data.dataArray.filter((d) => d._id === id); + const source = layer.getSource(); + const res = cloneDeep(source.data.dataArray.filter((d) => d._id === _id)); + if (res.length === 0) { + return res; + } + if (source.parser.type === 'geojson') { + const data = res.map((item) => { + const id = item[featureId]; + const feature = source.originData.features.find( + (item) => item[featureId] === id || item.properties[featureId] === id, + ); + const newFeature = cloneDeep(feature); + delete item.coordinates; + newFeature.properties = item; + return newFeature; + }); + return data; + } return res; } }