From dbc6de97123a026ad821e4e7348799147ac64932 Mon Sep 17 00:00:00 2001 From: Julie Wold Date: Wed, 7 Oct 2026 18:04:11 +0200 Subject: [PATCH] feat: add height on map --- src/components/MapLibreMap.tsx | 133 ++++++++++++++++++--------------- 1 file changed, 71 insertions(+), 62 deletions(-) diff --git a/src/components/MapLibreMap.tsx b/src/components/MapLibreMap.tsx index 53a52dc..00ffdf1 100644 --- a/src/components/MapLibreMap.tsx +++ b/src/components/MapLibreMap.tsx @@ -1,10 +1,10 @@ import { - LngLat, - type MapLayerMouseEvent, - type RequestTransformFunction, + LngLat, + type MapLayerMouseEvent, + type RequestTransformFunction, } from 'maplibre-gl'; import 'maplibre-gl/dist/maplibre-gl.css'; -import { RMap, useMap } from 'maplibre-react-components'; +import { RMap, RPopup, useMap } from 'maplibre-react-components'; import { getHoydeFromPunkt } from '../api/getHoydeFromPunkt'; import { useEffect, useState } from 'react'; import { Overlay } from './Overlay'; @@ -16,81 +16,90 @@ const TRONDHEIM_COORDS: [number, number] = [10.40565401, 63.4156575]; const KVP_BASE_URL = 'https://kvp.maps.norkart.no/mvt/'; type NorkartBasemapVariant = - | 'standard' - | 'standard-without-text' - | 'greyscale' - | 'greyscale-without-text' - | 'darkmode' - | 'transparent' - | 'hybrid' - | 'ortofoto'; + | 'standard' + | 'standard-without-text' + | 'greyscale' + | 'greyscale-without-text' + | 'darkmode' + | 'transparent' + | 'hybrid' + | 'ortofoto'; const NORKART_BASEMAP_VARIANT: NorkartBasemapVariant = 'standard'; const NORKART_BASEMAP_STYLE = `${KVP_BASE_URL}norkart-basemap/${NORKART_BASEMAP_VARIANT}/style.json`; export const MapLibreMap = () => { - const [pointHoyde, setPointHoydeAtPunkt] = useState( - undefined - ); + const [pointHoyde, setPointHoydeAtPunkt] = useState( + undefined + ); - const [address, setAddress] = useState
(null); + const [address, setAddress] = useState
(null); - const [clickPoint, setClickPoint] = useState(undefined); + const [clickPoint, setClickPoint] = useState(undefined); - useEffect(() => { - console.log(pointHoyde, clickPoint); - }, [clickPoint, pointHoyde]); + useEffect(() => { + console.log(pointHoyde, clickPoint); + }, [clickPoint, pointHoyde]); - const onMapClick = async (e: MapLayerMouseEvent) => { - const hoyder = await getHoydeFromPunkt(e.lngLat.lng, e.lngLat.lat); - setPointHoydeAtPunkt(hoyder[0].Z); - setClickPoint(new LngLat(e.lngLat.lng, e.lngLat.lat)); - }; + const onMapClick = async (e: MapLayerMouseEvent) => { + const hoyder = await getHoydeFromPunkt(e.lngLat.lng, e.lngLat.lat); + setPointHoydeAtPunkt(hoyder[0].Z); + setClickPoint(new LngLat(e.lngLat.lng, e.lngLat.lat)); + }; - return ( - - -

Dette er et overlay

-

Legg til funksjonalitet knyttet til kartet.

- -
- {address && ( - - )} - -
- ); + return ( + + +

Dette er et overlay

+

Legg til funksjonalitet knyttet til kartet.

+ +
+ {address && ( + + )} + + {clickPoint && pointHoyde !== undefined && ( + +
+

Latitude: {clickPoint.lat.toFixed(6)}

+

Longitude: {clickPoint.lng.toFixed(6)}

+

Height: {pointHoyde.toFixed(1)} m

+
+
+ )} +
+ ); }; function MapFlyTo({ lng, lat }: { lng: number; lat: number }) { - const map = useMap(); + const map = useMap(); - useEffect(() => { - map.flyTo({ center: [lng, lat], zoom: 20, speed: 10 }); - }, [lng, lat, map]); + useEffect(() => { + map.flyTo({ center: [lng, lat], zoom: 20, speed: 10 }); + }, [lng, lat, map]); - return null; + return null; } const transformRequest: RequestTransformFunction = (url) => { - if (!url.startsWith(KVP_BASE_URL)) { - return { url }; - } + if (!url.startsWith(KVP_BASE_URL)) { + return { url }; + } - const apiKey = import.meta.env.VITE_API_KEY; - const separator = url.includes('?') ? '&' : '?'; - return { url: `${url}${separator}api_key=${encodeURIComponent(apiKey)}` }; + const apiKey = import.meta.env.VITE_API_KEY; + const separator = url.includes('?') ? '&' : '?'; + return { url: `${url}${separator}api_key=${encodeURIComponent(apiKey)}` }; };