Newer
Older
TADOKOROc1231429 / gquuuuuux.html

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GNSS Logger with Polyline</title>
    <link rel="stylesheet" href="leaflet.css" />
    <style>
        #map {
            height: 600px;
            width: 100%;
        }
        #controls {
            margin-bottom: 10px;
        }
        #distance {
            margin-top: 10px;
            font-size: 20px;
        }
    </style>
</head>
<body>

<div id="controls">
    <button id="startButton">START</button>
    <button id="stopButton">STOP</button>
</div>
<div id="distance">距離: 0 m</div>
<div id="map"></div>

<script src="leaflet.js"></script>
<script>
    let map = L.map('map').setView([35.6762, 139.6503], 13);
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        maxZoom: 19,
        attribution: '© OpenStreetMap'
    }).addTo(map);

    let polyline = L.polyline([], { color: 'blue', weight: 3 }).addTo(map);
    let logging = false;
    let logInterval;
    let totalDistance = 0;

    document.getElementById('startButton').onclick = function() {
        if (!logging) {
            logging = true;
            logPosition();
            logInterval = setInterval(logPosition, 10000);
        }
    };

    document.getElementById('stopButton').onclick = function() {
        if (logging) {
            logging = false;
            clearInterval(logInterval);
        }
    };

    function logPosition() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                const lat = position.coords.latitude;
                const lng = position.coords.longitude;

                polyline.addLatLng([lat, lng]);
                map.setView([lat, lng], 13);

                if (polyline.getLatLngs().length > 1) {
                    const latlngs = polyline.getLatLngs();
                    const lastPoint = latlngs[latlngs.length - 2];
                    const newPoint = latlngs[latlngs.length - 1];
                    const distance = lastPoint.distanceTo(newPoint);
                    totalDistance += distance;
                    document.getElementById('distance').innerText = `距離: ${totalDistance.toFixed(2)} m`;
                }
            }, function(error) {
                console.error("Geolocation error: " + error.message);
            });
        } else {
            alert("このブラウザではGeolocationがサポートされていません。");
        }
    }
</script>

</body>
</html>