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

feat: show features without border & ability to add a custom marker #159

Merged
merged 2 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
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
42 changes: 40 additions & 2 deletions src/components/my-map/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { html, LitElement, unsafeCSS } from "lit";
import { customElement, property } from "lit/decorators.js";
import { Control, defaults as defaultControls } from "ol/control";
import { Point } from "ol/geom";
import { GeoJSON } from "ol/format";
import { Feature } from "ol/index";
import { defaults as defaultInteractions } from "ol/interaction";
import { Vector as VectorLayer } from "ol/layer";
import Map from "ol/Map";
import { fromLonLat, transformExtent } from "ol/proj";
import { Vector as VectorSource } from "ol/source";
import { Fill, Stroke, Style } from "ol/style";
import { Circle, Fill, Stroke, Style } from "ol/style";
import View from "ol/View";
import { last } from "rambda";

Expand Down Expand Up @@ -85,9 +87,24 @@ export class MyMap extends LitElement {
@property({ type: Boolean })
featureFill = false;

@property({ type: Boolean })
featureBorderNone = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I tend to favor positive naming schemes (e.g. featureBorder = true) as to avoid double negation (e.g. featureBorderNone = false).

Copy link
Member Author

Choose a reason for hiding this comment

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

See #37 - had originally defaulted to positive naming schemes for boolean properties, but then learned overtime that it doesn't play so nice with Lit so now consistently declaring all boolean props false by default.


@property({ type: Number })
featureBuffer = 40;

@property({ type: Boolean })
showMarker = false;

@property({ type: Number })
markerLatitude = this.latitude;

@property({ type: Number })
markerLongitude = this.longitude;

@property({ type: String })
markerColor = "#000000";

@property({ type: Object })
geojsonData = {
type: "FeatureCollection",
Expand Down Expand Up @@ -397,7 +414,8 @@ export class MyMap extends LitElement {

const outlineLayer = makeFeatureLayer(
this.featureColor,
this.featureFill
this.featureFill,
this.featureBorderNone
);
map.addLayer(outlineLayer);

Expand Down Expand Up @@ -430,6 +448,26 @@ export class MyMap extends LitElement {
});
}

// show a marker at a point
if (this.showMarker) {
const markerPoint = new Point(
fromLonLat([this.markerLongitude, this.markerLatitude])
);
const markerLayer = new VectorLayer({
source: new VectorSource({
features: [new Feature(markerPoint)],
}),
style: new Style({
image: new Circle({
radius: 9,
fill: new Fill({ color: this.markerColor }),
}),
}),
});

map.addLayer(markerLayer);
}

// XXX: force re-render for safari due to it thinking map is 0 height on load
setTimeout(() => {
window.dispatchEvent(new Event("resize"));
Expand Down
16 changes: 11 additions & 5 deletions src/components/my-map/os-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ const featureSource = new VectorSource();

export const outlineSource = new VectorSource();

export function makeFeatureLayer(color: string, featureFill: boolean) {
export function makeFeatureLayer(
color: string,
featureFill: boolean,
borderNone: boolean
) {
return new VectorLayer({
source: outlineSource,
style: new Style({
stroke: new Stroke({
width: 3,
color: color,
}),
stroke: borderNone
? undefined
: new Stroke({
width: 3,
color: color,
}),
fill: new Fill({
color: featureFill ? hexToRgba(color, 0.2) : hexToRgba(color, 0),
}),
Expand Down