diff --git a/map4.js b/map4.js index 926a447..2d8d097 100644 --- a/map4.js +++ b/map4.js @@ -24,7 +24,7 @@ let map; let selfMarker = null; let otherMarkers = []; -let latestByDevice = {}; // ★ オブジェクトとして利用 +let latestByDevice = {}; let allMembers = []; let lastLat = null; @@ -37,7 +37,7 @@ let stationMarkers = []; // =================================== -// ステータス:デフォルトは「移動中」 +// ステータス // =================================== let myStatus = localStorage.getItem("myStatus") || "移動中"; @@ -51,18 +51,17 @@ .eq("group_name", currentGroup) .eq("device_id", deviceId); - showOtherUsers(); // メンバーリスト更新 + showOtherUsers(); } function setupStatusButtons() { - const buttons = document.querySelectorAll(".stBtn"); - buttons.forEach((btn) => - btn.addEventListener("click", () => updateStatus(btn.dataset.status)) - ); + document.querySelectorAll(".stBtn").forEach((btn) => { + btn.addEventListener("click", () => updateStatus(btn.dataset.status)); + }); } // =================================== -// URL パラメータ読み込み +// URL パラメータ // =================================== function loadParams() { const p = new URLSearchParams(location.search); @@ -71,18 +70,10 @@ document.getElementById("groupName").textContent = currentGroup; document.getElementById("userName").textContent = currentUser; - - localStorage.setItem("group", currentGroup); - localStorage.setItem("userName", currentUser); - - const hostLabel = document.getElementById("hostStatus"); - if (hostLabel) { - hostLabel.textContent = isHost ? "あなたはホストです" : "一般メンバーです"; - } } // =================================== -// 現在位置取得 +// 現在位置 // =================================== function getPosition() { return new Promise((resolve, reject) => { @@ -95,7 +86,7 @@ } // =================================== -// 待ち合わせピン(violet) +// 待ち合わせピン // =================================== const meetIcon = L.icon({ iconUrl: @@ -116,7 +107,6 @@ maxZoom: 19, }).addTo(map); - // ホストだけ待ち合わせを設定 map.on("click", async (e) => { if (!isHost) return; @@ -124,8 +114,7 @@ const newLng = e.latlng.lng; if (targetLat !== null) { - const ok = confirm("待ち合わせ場所を置き直しますか?"); - if (!ok) return; + if (!confirm("待ち合わせ場所を置き直しますか?")) return; } targetLat = newLat; @@ -148,25 +137,26 @@ } // =================================== -// 自分の位置保存(ステータス込み) +// ★ 自分のピンを描画 // =================================== -async function saveMyLocation(lat, lng) { - await supa - .from("locations") - .delete() - .eq("group_name", currentGroup) - .eq("device_id", deviceId); +function renderSelfMarker() { + if (!map || lastLat === null || lastLng === null) return; - await supa.from("locations").insert({ - group_name: currentGroup, - user_name: currentUser, - device_id: deviceId, - lat, - lng, - status: myStatus, - }); + if (selfMarker) map.removeLayer(selfMarker); + + selfMarker = createLabeledMarker( + lastLat, + lastLng, + currentUser, + true, + myStatus + ); + + selfMarker.addTo(map); } - +// =================================== +// ピン(自分 & 他人) +// =================================== function createLabeledMarker(lat, lng, name, isSelf, status) { const pinUrl = isSelf ? "https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-green.png" @@ -214,21 +204,41 @@ icon: L.divIcon({ html, className: "", - iconSize: [70, 40], // ← 正しいサイズ(画像20px + 横ラベル) - iconAnchor: [10, 32], // ← ピンの「先端」が地図に刺さる位置 + iconSize: [70, 40], + iconAnchor: [10, 32], }), }); } // =================================== -// 距離計算(修正版) +// 自分の位置保存 +// =================================== +async function saveMyLocation(lat, lng) { + await supa + .from("locations") + .delete() + .eq("group_name", currentGroup) + .eq("device_id", deviceId); + + await supa.from("locations").insert({ + group_name: currentGroup, + user_name: currentUser, + device_id: deviceId, + lat, + lng, + status: myStatus, + }); +} + +// =================================== +// 距離計算 // =================================== function distanceMeters(lat1, lng1, lat2, lng2) { const R = 6371000; const toRad = (deg) => (deg * Math.PI) / 180; const dLat = toRad(lat2 - lat1); - const dLng = toRad(lng2 - lng1); // ← 正しい式 + const dLng = toRad(lng2 - lng1); const a = Math.sin(dLat / 2) ** 2 + @@ -253,7 +263,7 @@ } // =================================== -// 駅検索(Overpass) +// 駅検索(Overpass API) // =================================== async function fetchStationsAround(lat, lng) { const query = ` @@ -271,7 +281,7 @@ } // =================================== -// 最寄り駅のピン+表示 +// 最寄り駅のピン // =================================== async function loadStations() { const box = document.getElementById("nearestBox"); @@ -308,7 +318,7 @@ } // =================================== -// メンバー一覧表示(ステータス付き) +// メンバー一覧 // =================================== function showMemberList(latestByDevice) { const list = document.getElementById("memberList"); @@ -361,6 +371,7 @@ `; + // ★ メンバークリックでその位置へ移動 li.addEventListener("click", () => { const r = latestByDevice[member.device_id]; if (!r) return; @@ -379,8 +390,9 @@ "到着": "#9c27b0", }[s] || "#ccc"; } + // =================================== -// 他メンバーの位置表示 +// 他ユーザーのピン表示 // =================================== async function showOtherUsers() { const { data } = await supa @@ -399,18 +411,6 @@ otherMarkers.forEach((m) => map.removeLayer(m)); otherMarkers = []; - - Object.values(latestByDevice).forEach((r) => { - if (r.device_id === deviceId) return; - - const mk = createLabeledMarker(r.lat, r.lng, r.user_name, false, r.status); - mk.addTo(map); - otherMarkers.push(mk); - }); - - checkAllArrived(); -} - // =================================== // 待ち合わせ到着判定 // =================================== @@ -422,7 +422,7 @@ const ok = users.every((u) => { const d = distanceMeters(u.lat, u.lng, targetLat, targetLng); - return d < 120; + return d < 120; // 120m 以内を「到着」とみなす }); if (!ok) return; @@ -459,9 +459,7 @@ targetLng = data.lng; if (targetMarker) map.removeLayer(targetMarker); - targetMarker = L.marker([targetLat, targetLng], { icon: meetIcon }).addTo( - map - ); + targetMarker = L.marker([targetLat, targetLng], { icon: meetIcon }).addTo(map); document.getElementById("targetInfo").textContent = "待ち合わせ場所が共有されました!"; @@ -618,7 +616,9 @@ const pos = await getPosition(); lat = pos.latitude; lng = pos.longitude; - } catch {} + } catch { + // 失敗したら皇居のまま + } lastLat = lat; lastLng = lng; @@ -632,7 +632,7 @@ loadMessages(); loadStations(); - // 定期更新 + // ▼ 定期更新 setInterval(async () => { try { const pos = await getPosition(); @@ -652,10 +652,12 @@ setInterval(checkGroupActive, 4000); setInterval(loadStations, 15000); + // ▼ ボタン類 const exitBtn = document.getElementById("exitBtn"); if (exitBtn) { exitBtn.onclick = () => (location.href = "index.html"); } + const disbandBtn = document.getElementById("disbandBtn"); if (disbandBtn) { disbandBtn.onclick = disbandGroup; @@ -681,12 +683,18 @@ const g = p.get("group"); const u = p.get("user"); - const { data } = await supa + const { data, error } = await supa .from("groups") .select("*") .eq("group_name", g) .maybeSingle(); + if (error) { + alert("グループ情報の取得に失敗しました"); + location.href = "index.html"; + return; + } + if (!data) { alert("グループが存在しません"); location.href = "index.html"; @@ -695,7 +703,7 @@ if (data.is_active === false) { alert("このグループは解散されています"); - location.href = `archive.html?group=${g}`; + location.href = `archive.html?group=${encodeURIComponent(g)}`; return; } @@ -704,5 +712,26 @@ currentHostName = data.host_name; isHost = currentHostName === currentUser; + // 初期ホスト表示 + const hostLabel = document.getElementById("hostStatus"); + if (hostLabel) { + hostLabel.textContent = isHost ? "あなたはホストです" : "一般メンバーです"; + } + const disbandBtn = document.getElementById("disbandBtn"); + if (disbandBtn) { + disbandBtn.style.display = isHost ? "inline-block" : "none"; + } + main(); }); + + Object.values(latestByDevice).forEach((r) => { + if (r.device_id === deviceId) return; + + const mk = createLabeledMarker(r.lat, r.lng, r.user_name, false, r.status); + mk.addTo(map); + otherMarkers.push(mk); + }); + + checkAllArrived(); +}