diff --git a/map4.js b/map4.js index e9951dd..ca81a1c 100644 --- a/map4.js +++ b/map4.js @@ -37,25 +37,27 @@ } // ================================================== -// ラベル付きピン(名前常時表示) +// 安定するラベル付きピン // ================================================== function createLabeledMarker(lat, lng, name, isSelf) { + const iconHtml = ` +
+ +
${name}
+
+ `; + return L.marker([lat, lng], { icon: L.divIcon({ className: "", - html: ` -
- -
${name}
-
- `, + html: iconHtml, iconSize: [40, 55], iconAnchor: [20, 55], - }), + }) }); } @@ -116,28 +118,27 @@ Object.values(latestByDevice).forEach(row => { const li = document.createElement("li"); + // オンライン判定 const updated = new Date(row.updated_at); const diff = (Date.now() - updated) / 1000; const isOnline = diff < 30; li.classList.add(isOnline ? "online" : "offline"); - // 左側の丸アイコン + // 丸アイコン const icon = document.createElement("div"); icon.className = "member-icon"; - // 右側のテキスト(🟢オンライン) - const st = document.createElement("span"); - st.className = "status-text"; - st.textContent = isOnline ? "🟢オンライン" : "🔴オフライン"; + // 名前 + const name = document.createElement("span"); + name.textContent = row.user_name; - // 名前とステータスを配置 - const nameText = document.createElement("span"); - nameText.textContent = row.user_name; + // オンライン/オフライン文字 + const status = document.createElement("span"); + status.className = "status-text"; + status.textContent = isOnline ? "🟢オンライン" : "🔴オフライン"; - li.append(icon); - li.append(nameText); - li.append(st); + li.append(icon, name, status); // ピンへ移動 li.onclick = () => { @@ -162,10 +163,11 @@ .order("updated_at", { ascending: false }); latestByDevice = {}; - if (!Array.isArray(data)) return; - for (const r of data) { - if (!latestByDevice[r.device_id]) latestByDevice[r.device_id] = r; + if (Array.isArray(data)) { + for (const r of data) { + if (!latestByDevice[r.device_id]) latestByDevice[r.device_id] = r; + } } showMemberList(latestByDevice); @@ -195,9 +197,9 @@ let lat = DEFAULT_LAT, lng = DEFAULT_LNG; try { - const p = await getPosition(); - lat = p.latitude; - lng = p.longitude; + const pos = await getPosition(); + lat = pos.latitude; + lng = pos.longitude; } catch {} lastLat = lat; @@ -209,22 +211,27 @@ await showOtherUsers(); + // 位置更新 setInterval(async () => { try { - const p = await getPosition(); - lastLat = p.latitude; - lastLng = p.longitude; + const pos = await getPosition(); + lastLat = pos.latitude; + lastLng = pos.longitude; + renderSelfMarker(); await saveMyLocation(lastLat, lastLng); } catch {} }, 8000); + // 他ユーザー更新 setInterval(showOtherUsers, 2000); + // 退出 document.getElementById("exitBtn").onclick = () => (location.href = "index.html"); - setTimeout(() => map.invalidateSize(), 500); + // 中央ズレ対策 + setTimeout(() => map.invalidateSize(), 400); } window.addEventListener("DOMContentLoaded", main);