From 268f1c7d8262032029db34d26b90533a2a78233b Mon Sep 17 00:00:00 2001 From: xiongjj Date: Wed, 16 Oct 2024 18:09:23 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90feature=E3=80=91=20=E5=88=9B=E5=BB=BAl?= =?UTF-8?q?abel=E5=9B=BE=E5=B1=82=E6=97=B6=EF=BC=8C=E8=8B=A5text-field?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E5=AF=B9=E5=BA=94=E7=9A=84=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=EF=BC=8C=E5=88=99=E4=B8=8D=E5=88=9B=E5=BB=BA=E5=9B=BE=E5=B1=82?= =?UTF-8?q?;=20revie=20by=20qiw?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mapboxgl/web-map/WebMapViewModel.ts | 5 ++ .../web-map/__tests__/WebMapViewModel.spec.js | 57 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/mapboxgl/web-map/WebMapViewModel.ts b/src/mapboxgl/web-map/WebMapViewModel.ts index 04e2cb49..e321f499 100644 --- a/src/mapboxgl/web-map/WebMapViewModel.ts +++ b/src/mapboxgl/web-map/WebMapViewModel.ts @@ -1390,6 +1390,11 @@ export default class WebMapViewModel extends WebMapBase { private _addLabelLayer(layerInfo: any, features: any, addSource = false): void { const labelStyle = layerInfo.labelStyle; + const properties = features[0]?.properties; + const textField = labelStyle.labelField.replace(/{(.+)}/g, '$1'); + if (!properties || !properties[textField]) { + return; + } let { backgroundFill } = labelStyle; const fontFamily = labelStyle.fontFamily; const { minzoom, maxzoom } = layerInfo; diff --git a/src/mapboxgl/web-map/__tests__/WebMapViewModel.spec.js b/src/mapboxgl/web-map/__tests__/WebMapViewModel.spec.js index 1fb1ba04..b771f25f 100644 --- a/src/mapboxgl/web-map/__tests__/WebMapViewModel.spec.js +++ b/src/mapboxgl/web-map/__tests__/WebMapViewModel.spec.js @@ -1950,4 +1950,61 @@ describe('WebMapViewModel.spec', () => { }; viewModel.on({ addlayerssucceeded: callback }); }); + + it('label layer no label field', done => { + const layerInfo = { + layerType: 'UNIQUE', + visible: true, + themeSetting: {}, + name: 'labeNoField', + featureType: 'POINT', + labelStyle: { + offsetX: 0, + textBaseline: 'bottom', + fontFamily: '黑体', + offsetY: -19, + outlineWidth: 0, + textAlign: 'center', + outlineColor: '#000000', + fontSize: '14px', + fill: '#333', + backgroundFill: [255, 255, 255, 0.8], + labelField: '{未设置}' + }, + style: { + strokeWidth: 1, + offsetX: 0, + fillColor: '#E6F598', + offsetY: 0, + fillOpacity: 0.9, + radius: 15, + strokeColor: '#ffffff', + type: 'BASIC_POINT', + strokeOpacity: 1 + }, + projection: 'EPSG:4326', + enableFields: ['UserID'] + }; + const fetchResource = { + 'https://fakeiportal.supermap.io/iportal/web/datas/676516522/content.json?pageSize=9999999¤tPage=1&parentResType=MAP&parentResId=undefined': + layerData_CSV, + 'https://fakeiportal.supermap.io/iportal/web/datas/13136933/content.json?pageSize=9999999¤tPage=1&parentResType=MAP&parentResId=undefined': + layerData_geojson['POINT_GEOJSON'] + }; + mockFetch(fetchResource); + const id = { + ...uniqueLayer_point, + level: '', + visibleExtent: [0, 1, 2, 3] + }; + const callback = function (data) { + expect(data.layers.length).toBe(id.layers.length); + const spy = jest.spyOn(viewModel, '_addLayer'); + viewModel._addLabelLayer(layerInfo, [{ properties: {} }], true); + expect(spy).not.toBeCalled(); + done(); + }; + const viewModel = new WebMapViewModel(id, { ...commonOption }); + viewModel.on({ addlayerssucceeded: callback }); + }); });