Просмотр исходного кода

Haal locatie op via Geolocation API

Harry de Boer 6 лет назад
Родитель
Сommit
fd183f0943

+ 18 - 0
src/main/resources/static/index.html

@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html lang="nl">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Puzzeltocht</title>
+    <script src="main.js"></script>
+    <link rel="stylesheet" href="main.css">
+</head>
+<body>
+    <header>
+        <h1>Summercamp puzzeltocht</h1>
+    </header>
+    <div id="unsupported">Deze browser ondersteunt de vereiste functionaliteit voor deze applicatie</div>
+    <div id="location">Locatie onbekend</div>
+    <img id="map" alt="Kaart" src="" />
+</body>
+</html>

+ 26 - 0
src/main/resources/static/main.css

@@ -0,0 +1,26 @@
+html, body {
+    height: 100vh;
+    overflow: hidden;
+    margin: 0; padding: 0;
+    color: black;
+    background-color: white;
+    box-sizing: border-box;
+    font-family: "Times New Roman", Times, serif;
+    line-height: 1.15;
+}
+
+*, *:before, *:after {
+    box-sizing: inherit;
+}
+
+header {
+    margin: 0;
+    padding: 1vh;
+    background-color: #444;
+    color: #eee;
+    font-family: Arial, Helvetica, sans-serif;
+}
+
+#unsupported {
+    display: none;
+}

+ 50 - 0
src/main/resources/static/main.js

@@ -0,0 +1,50 @@
+"use strict";
+
+let p = undefined;
+window.addEventListener("DOMContentLoaded", () => {
+    p = new Puzzeltocht();
+});
+
+class Puzzeltocht {
+
+    constructor() {
+        if ("geolocation" in navigator) {
+            console.log("Geolocation API available");
+        } else {
+            console.log("Geolocation API not available");
+            document.getElementById("unsupported").style.display = "block";
+        }
+
+        if (window.isSecureContext) {
+            console.log("Secure context available");
+        } else {
+            console.log("Secure context not available");
+            document.getElementById("unsupported").style.display = "block";
+        }
+
+        navigator.geolocation.getCurrentPosition((pos) => Puzzeltocht.updateLocation(pos));
+        navigator.geolocation.watchPosition((pos) => Puzzeltocht.updateLocation(pos));
+
+        console.log("puzzeltocht initialised");
+    }
+
+    static updateLocation(pos) {
+        let l = document.getElementById("location");
+        let lat = pos.coords.latitude;
+        let long = pos.coords.longitude;
+        let ts = new Date();
+        ts.setTime(pos.timestamp);
+        l.innerText = "positie: " + lat + ", "  + long + ", " + pos.coords.accuracy + " (updated " + ts.toUTCString() + ")";
+        Puzzeltocht.updateMap(pos);
+    }
+
+    static updateMap(pos) {
+        let img = document.getElementById("map");
+        let lat = pos.coords.latitude;
+        let long = pos.coords.longitude;
+        let url = "https://staticmap.openstreetmap.de/staticmap.php?center=";
+        url = url + lat + "," + long + "&zoom=18&size=256x256&maptype=mapnik";
+        url = url + "&markers=" + lat + "," + long + ",lightblue1";
+        img.src = url;
+    }
+}