Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set color of polygon with color set in the geojson feature properties #381

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was struggling to add the specific type for feature.
Tried adding feature: Feature and feature: typeof Feature with no luck even though this is an instance of Feature

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries here, OpenLayers can be finiky about this - it might be:

import { Feature } from "ol/index";

style: function (this: MyMap, feature: Feature<Geometry>) {...}

But if that still complains, feel free to leave this as any and I can re-visit later!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah still complaining 😕 .
Let me know if you do figure this one out, i'm intrigued now !

// Retrieve color from feature properties
let featureColor = feature.get("color") || this.geojsonColor; // Use the geojsonColor if no color property exists
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌 smart & simple !


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