From fb31a822a09568468e71ebbf7004d6c6c9203c32 Mon Sep 17 00:00:00 2001 From: log101 Date: Sun, 21 Jul 2024 11:45:55 +0300 Subject: [PATCH] feat: add new control to leaflet web component --- src/components/LeafletMap/controls.ts | 19 +++++++++ src/components/LeafletMap/icons.ts | 13 ++++++ src/components/leaflet-map.ts | 57 ++++++++++++++++++++++----- src/pages/[id].astro | 9 +---- 4 files changed, 80 insertions(+), 18 deletions(-) create mode 100644 src/components/LeafletMap/controls.ts create mode 100644 src/components/LeafletMap/icons.ts diff --git a/src/components/LeafletMap/controls.ts b/src/components/LeafletMap/controls.ts new file mode 100644 index 0000000..abde188 --- /dev/null +++ b/src/components/LeafletMap/controls.ts @@ -0,0 +1,19 @@ +import L from "leaflet"; + +const TargetLocationControl = L.Control.extend({ + onAdd: function (map: L.Map, targetLocation: L.LatLngTuple) { + const locationButton = document.createElement("button"); + + locationButton.textContent = "Hedefe Git"; + + locationButton.classList.add("custom-map-control-button"); + + locationButton.addEventListener("click", () => { + map.setView(targetLocation, 18); + }); + + return locationButton; + }, +}); + +export { TargetLocationControl }; diff --git a/src/components/LeafletMap/icons.ts b/src/components/LeafletMap/icons.ts new file mode 100644 index 0000000..a9c0415 --- /dev/null +++ b/src/components/LeafletMap/icons.ts @@ -0,0 +1,13 @@ +import { icon } from "leaflet"; + +var targetLocationIcon = icon({ + iconUrl: "goal.svg", + iconSize: [32, 32], +}); + +var currentLocationIcon = icon({ + iconUrl: "blue-dot.png", + iconSize: [32, 32], +}); + +export { targetLocationIcon, currentLocationIcon }; diff --git a/src/components/leaflet-map.ts b/src/components/leaflet-map.ts index 92911c0..f2b2ea0 100644 --- a/src/components/leaflet-map.ts +++ b/src/components/leaflet-map.ts @@ -1,18 +1,34 @@ // Lit -import { html, LitElement } from "lit"; +import { html, LitElement, unsafeCSS, type CSSResultGroup } from "lit"; import { customElement, property, query, state } from "lit/decorators.js"; // Leaflet -import L from "leaflet"; +import L, { Map } from "leaflet"; import type { LatLngTuple } from "leaflet"; +import { targetLocationIcon } from "./LeafletMap/icons"; +import { TargetLocationControl } from "./LeafletMap/controls"; + +// Styles +import leafletStyles from "leaflet/dist/leaflet.css?inline"; +import globalStyles from "@/styles/globals.css?inline"; +import mapStyles from "@/styles/locked-page.css?inline"; @customElement("leaflet-map") export class LeafletMap extends LitElement { - @property({ type: Object }) targetLocation?: LatLngTuple; + // Styles + static styles?: CSSResultGroup | undefined = [ + unsafeCSS(leafletStyles), + unsafeCSS(globalStyles), + unsafeCSS(mapStyles), + ]; - @query("#leaflet-map-container") + // Div element to initialize Leaflet in + @query("#mapid") _mapElement!: HTMLDivElement; + // Properties and states + @property({ type: Object }) targetLocation?: LatLngTuple; + @state() protected _map?: L.Map; @state() @@ -22,8 +38,32 @@ export class LeafletMap extends LitElement { @state() protected _watchingLocation = false; - connectedCallback(): void { - super.connectedCallback(); + firstUpdated(): void { + if (!this._mapElement || !this.targetLocation) return; + var map = new Map(this._mapElement).setView(this.targetLocation, 13); + + L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", { + maxZoom: 19, + attribution: + '© OpenStreetMap', + }).addTo(map); + + // Add target location icon marker + L.marker(this.targetLocation, { icon: targetLocationIcon }).addTo(map); + + L.circle(this.targetLocation, { + color: "blue", + fillColor: "#30f", + fillOpacity: 0.2, + radius: 50, + }).addTo(map); + + // Add target location control + const targetLocationControl = new TargetLocationControl({ + position: "bottomleft", + }); + + targetLocationControl.addTo(map); // Check geolocation permission, if user has given permission before // start watching user location @@ -44,9 +84,6 @@ export class LeafletMap extends LitElement { } render() { - return html`
`; + return html`
`; } } diff --git a/src/pages/[id].astro b/src/pages/[id].astro index d10b62b..6eeb904 100644 --- a/src/pages/[id].astro +++ b/src/pages/[id].astro @@ -71,13 +71,6 @@ const dateFromNow = dayjs.utc(data?.created_at).from(dayjs.utc()); -
-
-

@@ -85,7 +78,7 @@ const dateFromNow = dayjs.utc(data?.created_at).from(dayjs.utc());

- +