summaryrefslogtreecommitdiff
path: root/chaski/ilztal.live/geolocation/mapview.html
blob: bb62f62b5cb4a3668a15f48b1f207d28854f129b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!doctype html>
<html class="no-js" lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title>Position view</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="apple-touch-icon" href="/apple-touch-icon.png">
        <!-- Place favicon.ico in the root directory -->

        <link rel="stylesheet" href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css"
              integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
              crossorigin=""/>
        <script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js"
                integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
                crossorigin=""></script>

    </head>
    <body>
        <!--[if lt IE 8]>
            <p class="browserupgrade">
            You are using an <strong>outdated</strong> browser. Please
            <a href="http://browsehappy.com/">upgrade your browser</a> to improve
            your experience.
            </p>
        <![endif]-->

        <div align="justify">
          <div id="MapID2" style="height:100vh; margin-left: auto; margin-right:auto; width:80%"></div>
        </div>
        <script>
         var mymap = L.map('MapID2');
         L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoiZmVsaXg4MTgyOTMiLCJhIjoiY2p6a2h6cjdyMGpicjNvbzlzZ3UwNmloMCJ9.a4t1KM9Gid-q29ultM7HgA', {
             attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
             maxZoom: 18,
             id: 'mapbox/streets-v11',
             accessToken: 'pk.eyJ1IjoiZmVsaXg4MTgyOTMiLCJhIjoiY2p6a2h6cjdyMGpicjNvbzlzZ3UwNmloMCJ9.a4t1KM9Gid-q29ultM7HgA'
         }).addTo(mymap);
         var marker = L.marker([0,0]).addTo(mymap);
         marker.bindPopup("Hello Train!");

         mymap.setView([0,0], 16);
         function updateMap(pos) {
             if (pos === null) {
                 console.log("no data yet?");
                 return;
             }
             mymap.setView(pos);
             mymap.scrollWheelZoom.disable();
             marker.setLatLng(pos);
         }
         updateMap([48.13716,11.57540]);

         function fetchUpdate () {
             fetch("https://ilztal.live/geoloc/history")
                 .then((data) => {
                     let text = data.text().then(text => {
                         let hist = JSON.parse(text);
                         console.log(hist)

                         let latlngs = hist.filter(e => e.data.latitude !== undefined).map((e) => [e.data.latitude, e.data.longitude]);

                         console.log(latlngs);
                         let polyline = L.polyline(latlngs, {color: "red"}).addTo(mymap);

                         mymap.fitBounds(polyline.getBounds())

                         for (idx in hist) {
                             if (hist[idx].data.latitude === undefined) {
                                 console.log(hist[idx].data, idx, 1+parseInt(idx))
                                 let nidx = 1+parseInt(idx);
                                 if (hist[nidx] !== undefined && hist[nidx].data.latitude !== undefined) {
                                     let marker = L.marker([hist[nidx].data.latitude, hist[nidx].data.longitude])
                                                   .addTo(mymap);
                                     marker.bindPopup(hist[idx].data);
                                 }
                             }
                         }
                     });

                 })
                 .catch((e) => {
                     console.log("some error!", e);
                     setTimeout(fetchUpdate, 3000);
                 })
         }

         fetchUpdate()
        </script>

    </body>
</html>