Newer
Older
TADOKOROc1231429 / kyorikyori.html
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="leaflet.css" />
    <script src="leaflet.js"></script>
    <script src="https://unpkg.com/leaflet-geometryutil@0.10.1/src/leaflet.geometryutil.js"></script>
    <title>酒田市面積計算ゲーム</title>
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            height: 100vh;
        }
        #map {
            flex-grow: 1;
        }
        #controls {
            background: #f8f9fa;
            padding: 10px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            z-index: 1000;
        }
        #info, #count {
            margin-bottom: 10px;
            font-size: 16px;
        }
        #calculateButton {
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        #calculateButton:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>

<div id="controls">
    <div id="info">現在の面積: 0 平方メートル</div>
    <div id="count">計算回数: 0</div>
    <button id="calculateButton">面積を計算</button>
</div>
<div id="map"></div>

<script>
    const map = L.map('map').setView([38.25, 139.83], 8);
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        maxZoom: 19,
        attribution: '© OpenStreetMap'
    }).addTo(map);

    const markers = [];
    let polyline;
    let polygon;
    let calculationCount = 0;

    map.on('click', function(e) {
        const marker = L.marker(e.latlng).addTo(map);
        markers.push(e.latlng);

        if (markers.length > 1) {
            updatePolygon();
        }

        marker.on('click', function() {
            map.removeLayer(marker);
            markers.splice(markers.indexOf(e.latlng), 1);
            updatePolygon();
        });
    });

    document.getElementById('calculateButton').addEventListener('click', calculateArea);

    function updatePolygon() {
        if (polyline) map.removeLayer(polyline);
        if (polygon) map.removeLayer(polygon);

        const latlngs = markers.map(latlng => [latlng.lat, latlng.lng]);
        
        if (markers.length > 1) {
            polyline = L.polyline(latlngs.concat([latlngs[0]]), { color: 'blue' }).addTo(map);
        }
        
        if (markers.length > 2) {
            polygon = L.polygon(latlngs, { color: 'blue', fillColor: 'rgba(0, 0, 255, 0.3)', fillOpacity: 0.5 }).addTo(map);
        }
    }

    function calculateArea() {
        if (markers.length < 3) {
            alert("3つ以上のポイントが必要です。");
            return;
        }

        const latlngs = markers.map(latlng => [latlng.lat, latlng.lng]);
        const area = L.GeometryUtil.geodesicArea(latlngs);
        const areaInSquareMeters = Math.round(area);

        document.getElementById('info').innerText = `現在の面積: ${areaInSquareMeters} 平方メートル`;

        calculationCount++;
        document.getElementById('count').innerText = `計算回数: ${calculationCount}`;

        if (areaInSquareMeters >= 602980000) {
            alert("クリア! 酒田市の面積に到達しました!");
            resetMap();
        }
    }

    function resetMap() {
        markers.forEach(marker => map.removeLayer(marker));
        markers.length = 0;
        if (polyline) map.removeLayer(polyline);
        if (polygon) map.removeLayer(polygon);
        document.getElementById('info').innerText = "現在の面積: 0 平方メートル";
        calculationCount = 0;
        document.getElementById('count').innerText = "計算回数: 0";
    }
</script>

</body>
</html>