From badfac19973179f3c5e155e181c4c90e8ab50429 Mon Sep 17 00:00:00 2001 From: Benjamin Baumann Date: Thu, 31 Aug 2023 12:30:52 +0100 Subject: [PATCH] Set color of polygon with color set in the geojson feature properties - Default to geojsonColor is this properties color object does not exist --- src/components/my-map/index.ts | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/components/my-map/index.ts b/src/components/my-map/index.ts index f532185f..95a81bd9 100644 --- a/src/components/my-map/index.ts +++ b/src/components/my-map/index.ts @@ -374,17 +374,22 @@ export class MyMap extends LitElement { const geojsonLayer = new VectorLayer({ source: geojsonSource, - style: new Style({ - stroke: new Stroke({ - color: this.geojsonColor, - width: 3, - }), - fill: new Fill({ - color: this.geojsonFill - ? hexToRgba(this.geojsonColor, 0.2) - : hexToRgba(this.geojsonColor, 0), - }), - }), + style: function (this: MyMap, feature: any) { + // Retrieve color from feature properties + let featureColor = feature.get("color") || this.geojsonColor; // Use the geojsonColor if no color property exists + + return new Style({ + stroke: new Stroke({ + color: featureColor, + width: 3, + }), + fill: new Fill({ + color: this.geojsonFill + ? hexToRgba(featureColor, 0.2) + : hexToRgba(featureColor, 0), + }), + }); + }.bind(this), }); map.addLayer(geojsonLayer);