diff --git a/map4.js b/map4.js index e6d731a..532973f 100644 --- a/map4.js +++ b/map4.js @@ -3,7 +3,7 @@ // =================================== const SUPABASE_URL = "https://ogtlmtnjkpsxsqzqlacj.supabase.co"; const SUPABASE_KEY = - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im9ndGxtdG5qa3BzeHNxenFsYWNqIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjMyOTU3NjUsImV4cCI6MjA3ODg3MTc2NX0.JnCE7oUQwrSgGqiu-QRbwnaLBZrO8JX1_RUb37VIMFI"; +"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9....VIMFI"; const supa = window.supabase.createClient(SUPABASE_URL, SUPABASE_KEY); @@ -16,6 +16,7 @@ let currentGroup = ""; let currentUser = ""; + let map; let selfMarker = null; let otherMarkers = []; @@ -28,6 +29,8 @@ const DEFAULT_LNG = 139.752799; // =================================== +// URL 読み込み +// =================================== function loadParams() { const p = new URLSearchParams(location.search); currentGroup = p.get("group"); @@ -38,18 +41,21 @@ } // =================================== +// アイコン +// =================================== function createIcon(type) { return L.icon({ - iconUrl: - type === "self" - ? "https://maps.gstatic.com/mapfiles/kml/paddle/blu-stars.png" - : "https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-green.png", + iconUrl: type === "self" + ? "https://maps.gstatic.com/mapfiles/kml/paddle/blu-stars.png" + : "https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-green.png", iconSize: [40, 55], iconAnchor: [20, 50], }); } // =================================== +// GPS +// =================================== function getPosition() { return new Promise((res, rej) => { navigator.geolocation.getCurrentPosition( @@ -61,6 +67,8 @@ } // =================================== +// 地図初期化 +// =================================== function initMap(lat, lng) { map = L.map("map").setView([lat, lng], 15); @@ -70,6 +78,8 @@ } // =================================== +// 自分のピン +// =================================== function renderSelfMarker() { if (selfMarker) map.removeLayer(selfMarker); @@ -79,6 +89,7 @@ selfMarker.bindPopup(`${currentUser}(あなた)`); + // ★ 中心移動は e.latlng を必ず使う selfMarker.on("click", (e) => { map.panTo(e.latlng, { animate: true, duration: 0.4 }); selfMarker.openPopup(); @@ -86,6 +97,8 @@ } // =================================== +// DB 保存 +// =================================== async function saveMyLocation(lat, lng) { await supa.from("locations") .delete() @@ -102,6 +115,8 @@ } // =================================== +// メンバー一覧 +// =================================== function showMemberList(latestByDevice) { const list = document.getElementById("memberList"); list.innerHTML = ""; @@ -114,11 +129,10 @@ const icon = document.createElement("div"); icon.className = "member-icon"; - li.appendChild(icon); + li.append(icon); li.append(row.user_name); - // ★ ピンの click を発火(中央移動100%) li.addEventListener("click", () => { if (row.device_id === deviceId) { if (selfMarker) selfMarker.fire("click"); @@ -144,7 +158,9 @@ if (!Array.isArray(data)) return; for (const row of data) { - if (!latestByDevice[row.device_id]) latestByDevice[row.device_id] = row; + if (!latestByDevice[row.device_id]) { + latestByDevice[row.device_id] = row; + } } showMemberList(latestByDevice); @@ -173,6 +189,8 @@ } // =================================== +// main +// =================================== async function main() { loadParams(); @@ -198,7 +216,6 @@ const pos = await getPosition(); lastLat = pos.latitude; lastLng = pos.longitude; - renderSelfMarker(); await saveMyLocation(lastLat, lastLng); } catch {} @@ -209,8 +226,10 @@ document.getElementById("exitBtn").onclick = () => (location.href = "index.html"); - // ★ Leaflet の中心ズレを完全修正 - setTimeout(() => map.invalidateSize(), 300); + // ★ 地図サイズの最終調整(超重要) + setTimeout(() => { + map.invalidateSize(); + }, 500); } window.addEventListener("DOMContentLoaded", main);