Skip to content

Commit

Permalink
Set color of polygon with color set in the geojson feature properties (
Browse files Browse the repository at this point in the history
…#381)

- Default to geojsonColor is this properties color object does not exist

Co-authored-by: Benjamin Baumann <[email protected]>
  • Loading branch information
benbaumann95 and Benjamin Baumann authored Sep 1, 2023
1 parent 6b43de4 commit ff1e90c
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/components/my-map/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ff1e90c

Please sign in to comment.