34 lines
935 B
TypeScript
34 lines
935 B
TypeScript
import proj4 from "proj4";
|
|
import type { Point } from "../karte/types";
|
|
|
|
const ETRS25832 =
|
|
"+proj=utm +zone=32 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs";
|
|
|
|
proj4.defs("EPSG:25832", ETRS25832);
|
|
|
|
export function toEtrs(lon: number, lat: number): Point {
|
|
const projected = proj4("EPSG:4326", "EPSG:25832", [lon, lat]);
|
|
return { easting: projected[0], northing: projected[1] };
|
|
}
|
|
|
|
export function watchStandort(
|
|
onStandort: (standort: Point | "denied") => void,
|
|
): () => void {
|
|
if (!navigator.geolocation) {
|
|
onStandort("denied");
|
|
return () => {};
|
|
}
|
|
|
|
const id = navigator.geolocation.watchPosition(
|
|
(position) => {
|
|
onStandort(toEtrs(position.coords.longitude, position.coords.latitude));
|
|
},
|
|
(error) => {
|
|
if (error.code === error.PERMISSION_DENIED) onStandort("denied");
|
|
},
|
|
{ enableHighAccuracy: true },
|
|
);
|
|
|
|
return () => navigator.geolocation.clearWatch(id);
|
|
}
|