";
}
function getPopupOptions() {
var isNarrow = window.matchMedia("(max-width: 640px)").matches;
var padLeft = isNarrow ? 8 : 56;
var padTop = isNarrow ? 8 : 20;
var padRight = isNarrow ? 8 : 56;
var padBottom = isNarrow ? 8 : 64;
if (!isNarrow) {
var leftStack = document.querySelector("#map .leaflet-top.leaflet-left");
if (leftStack) {
var box = leftStack.getBoundingClientRect();
if (box.width > 0) {
padLeft = Math.ceil(box.width) + 16;
}
}
}
return {
maxWidth: getPopupMaxWidth(),
minWidth: 0,
className: "chain-leaflet-popup",
// 左矢印(約8px)分だけ右へずらして先端をマーカーへ寄せる
offset: L.point(10, 0),
autoPan: false,
keepInView: false,
autoPanPaddingTopLeft: L.point(padLeft, padTop),
autoPanPaddingBottomRight: L.point(padRight, padBottom)
};
}
// Leaflet既定は上開き(tipは下中央)。
// CSSで tip を左中央に移すので、位置計算も右開きに合わせる
function enablePopupOpenRight(popup) {
if (!popup || popup._chainOpenRight) {
return;
}
popup._chainOpenRight = true;
popup._updatePosition = function () {
if (!this._map || !this._container) {
return;
}
var pos = this._map.latLngToLayerPoint(this._latlng);
var offset = L.point(this.options.offset || [0, 0]);
var anchor = L.point(0, 0);
if (this._source && typeof this._source._getPopupAnchor === "function") {
anchor = L.point(this._source._getPopupAnchor());
}
// transform 原点をマーカー(+popupAnchor)に置く
L.DomUtil.setPosition(this._container, pos.add(anchor));
var height = this._container.offsetHeight;
this._containerWidth = this._container.offsetWidth;
// tipは左側・縦中央。左端をマーカー近くに置き、縦は中央揃え
// Leaflet: topY = pos.y - height - containerBottom
this._containerLeft = offset.x;
this._containerBottom = -Math.round(height / 2);
this._container.style.left = this._containerLeft + "px";
this._container.style.bottom = this._containerBottom + "px";
};
}
// 実描画後の矩形でマップ内に収まるようパン(1回にまとめてカクつき防止)
function ensurePopupInMap(popup, options) {
if (!popup || !popup._map || !popup._container) {
return;
}
if (typeof popup.isOpen === "function" && !popup.isOpen()) {
return;
}
options = options || {};
if (typeof popup._updateLayout === "function") {
popup._updateLayout();
}
if (typeof popup._updatePosition === "function") {
popup._updatePosition();
}
var mapInstance = popup._map;
var mapRect = mapInstance.getContainer().getBoundingClientRect();
var popRect = popup._container.getBoundingClientRect();
if (!mapRect.width || !popRect.width) {
return;
}
var padTL = L.point(popup.options.autoPanPaddingTopLeft || [8, 8]);
var padBR = L.point(popup.options.autoPanPaddingBottomRight || [8, 8]);
var dx = 0;
var dy = 0;
if (popRect.right + padBR.x > mapRect.right) {
dx = popRect.right + padBR.x - mapRect.right;
}
if (popRect.left - dx < mapRect.left + padTL.x) {
dx = popRect.left - (mapRect.left + padTL.x);
}
if (popRect.bottom + padBR.y > mapRect.bottom) {
dy = popRect.bottom + padBR.y - mapRect.bottom;
}
if (popRect.top - dy < mapRect.top + padTL.y) {
dy = popRect.top - (mapRect.top + padTL.y);
}
if (dx || dy) {
mapInstance.panBy([dx, dy], { animate: options.animate !== false });
}
}
function schedulePopupInMap(popup) {
if (!popup) {
return;
}
// 同一オープン中の多重パンを避ける
if (popup._chainPanTimer) {
clearTimeout(popup._chainPanTimer);
popup._chainPanTimer = null;
}
if (popup._chainPanRaf) {
if (typeof window.cancelAnimationFrame === "function") {
window.cancelAnimationFrame(popup._chainPanRaf);
}
popup._chainPanRaf = null;
}
function runOnce() {
popup._chainPanRaf = null;
popup._chainPanTimer = null;
ensurePopupInMap(popup, { animate: true });
}
// レイアウト確定後に1回だけスムーズパン
if (typeof window.requestAnimationFrame === "function") {
popup._chainPanRaf = window.requestAnimationFrame(function () {
popup._chainPanRaf = window.requestAnimationFrame(runOnce);
});
} else {
popup._chainPanTimer = setTimeout(runOnce, 32);
}
}
function schedulePopupInMapAfterImages(popup) {
if (!popup || !popup._container) {
return;
}
var imgs = popup._container.querySelectorAll("img");
var pending = 0;
function onDone() {
pending -= 1;
if (pending > 0) {
return;
}
// 画像読込完了後も1回だけ(短くデバウンス)
if (popup._chainImgPanTimer) {
clearTimeout(popup._chainImgPanTimer);
}
popup._chainImgPanTimer = setTimeout(function () {
popup._chainImgPanTimer = null;
ensurePopupInMap(popup, { animate: true });
}, 80);
}
for (var i = 0; i < imgs.length; i++) {
if (imgs[i].complete) {
continue;
}
pending += 1;
imgs[i].addEventListener("load", onDone);
imgs[i].addEventListener("error", onDone);
}
}
function kpBetween(kp, from, to) {
var min = Math.min(from, to);
var max = Math.max(from, to);
return kp >= min && kp <= max;
}
function polylineByKp(kpList, from, to) {
var coords = [];
for (var i = 0; i < kpList.length; i++) {
var kp = parseFloat(kpList[i].kp);
if (kpBetween(kp, from, to)) {
coords.push([parseFloat(kpList[i].lat), parseFloat(kpList[i].lng)]);
}
}
return coords;
}
function nearestKpIndex(kpList, lat, lng) {
var best = 0;
var bestDist = Infinity;
for (var i = 0; i < kpList.length; i++) {
var dLat = parseFloat(kpList[i].lat) - lat;
var dLng = (parseFloat(kpList[i].lng) - lng) * 0.8;
var dist = dLat * dLat + dLng * dLng;
if (dist < bestDist) {
bestDist = dist;
best = i;
}
}
return best;
}
function iconBox(latlng, icon, pad) {
var size = icon.options.iconSize;
var anchor = icon.options.iconAnchor || [size[0] / 2, size[1] / 2];
var pt = map.latLngToLayerPoint(latlng);
return {
left: pt.x - anchor[0] - pad,
top: pt.y - anchor[1] - pad,
right: pt.x - anchor[0] + size[0] + pad,
bottom: pt.y - anchor[1] + size[1] + pad
};
}
function boxesOverlap(a, b) {
return a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom;
}
function hitsAny(latlng, icon, boxes) {
var box = iconBox(latlng, icon, 0);
for (var i = 0; i < boxes.length; i++) {
if (boxesOverlap(box, boxes[i])) {
return true;
}
}
return false;
}
var ROUTE_SHIFT_STRIDE = 3;
var ROUTE_SHIFT_MAX = 200;
var MARKER_GAP = 3;
var ROUTE_ICON_BASE = 36;
var ROUTE_ICON_SCALES = [1, 0.85, 0.7, 0.6, 0.5];
function routeIconAtScale(isR8, scale) {
var size = Math.max(1, Math.round(ROUTE_ICON_BASE * scale));
var url = wp_style_uri + "/sekisetu/icon/" + (isR8 ? "R8.svg" : "R18.svg");
return L.icon({
iconUrl: url,
iconRetinaUrl: url,
iconSize: [size, size],
iconAnchor: [size / 2, Math.round(size * 2 / 3)],
className: "leaflet-terminator"
});
}
function relocateRouteMarkers() {
if (!map || !routeMarkers.length) {
return;
}
var obstacles = [];
var i;
for (i = 0; i < pointMarkers.length; i++) {
obstacles.push(iconBox(pointMarkers[i].getLatLng(), pointMarkers[i].options.icon, MARKER_GAP));
}
for (i = 0; i < sapaMarkers.length; i++) {
obstacles.push(iconBox(sapaMarkers[i].getLatLng(), sapaMarkers[i].options.icon, MARKER_GAP));
}
for (i = 0; i < routeMarkers.length; i++) {
var marker = routeMarkers[i];
var isR8 = marker.homeRoute === "8";
var kpList = isR8 ? kp8_100 : kp18_100;
var placed = null;
var placedScale = ROUTE_ICON_SCALES[ROUTE_ICON_SCALES.length - 1];
var placedIcon = null;
for (var si = 0; si < ROUTE_ICON_SCALES.length && !placed; si++) {
var scale = ROUTE_ICON_SCALES[si];
var icon = routeIconAtScale(isR8, scale);
for (var step = 0; step <= ROUTE_SHIFT_MAX && !placed; step += ROUTE_SHIFT_STRIDE) {
var offsets = step === 0 ? [0] : [step, -step];
for (var d = 0; d < offsets.length; d++) {
var idx = marker.homeIndex + offsets[d];
if (idx < 0 || idx >= kpList.length) {
continue;
}
var candidate = L.latLng(parseFloat(kpList[idx].lat), parseFloat(kpList[idx].lng));
if (!hitsAny(candidate, icon, obstacles)) {
placed = candidate;
placedScale = scale;
placedIcon = icon;
break;
}
}
}
}
if (!placed) {
placed = marker.homeLatLng;
placedScale = ROUTE_ICON_SCALES[ROUTE_ICON_SCALES.length - 1];
placedIcon = routeIconAtScale(isR8, placedScale);
}
marker.setIcon(placedIcon);
marker.setLatLng(placed);
marker._routeIconScale = placedScale;
obstacles.push(iconBox(placed, placedIcon, MARKER_GAP));
}
}
function clearDynamicLayers() {
for (var i = 0; i < troubleLayers.length; i++) {
map.removeLayer(troubleLayers[i]);
}
troubleLayers = [];
for (var j = 0; j < pointMarkers.length; j++) {
map.removeLayer(pointMarkers[j]);
}
pointMarkers = [];
}
function expresswayIcIcon(name, showLabel, labelSide) {
var html = '';
if (showLabel) {
var sideClass = "";
if (labelSide === "left") {
sideClass = " is-left";
} else if (labelSide === "bottom") {
sideClass = " is-bottom";
} else if (labelSide === "top") {
sideClass = " is-top";
}
html += '' + name + "";
}
return L.divIcon({
className: "expressway-ic-marker leaflet-terminator" + (showLabel ? "" : " labels-hidden"),
html: html,
iconSize: [14, 14],
iconAnchor: [7, 7]
});
}
function updateDensityLayers() {
if (!map) {
return;
}
var z = map.getZoom();
var showIcLabel = z >= IC_LABEL_MIN_ZOOM;
var showSapaLabel = z >= SAPA_LABEL_MIN_ZOOM;
var i;
for (i = 0; i < expresswayIcMarkers.length; i++) {
var ic = expresswayIcMarkers[i];
if (ic._showLabel === showIcLabel) {
continue;
}
ic._showLabel = showIcLabel;
ic.setIcon(expresswayIcIcon(ic._icName, showIcLabel, ic._labelSide));
}
for (i = 0; i < sapaMarkers.length; i++) {
var sm = sapaMarkers[i];
if (sm._sapaLabelOn === showSapaLabel) {
continue;
}
sm._sapaLabelOn = showSapaLabel;
sm.unbindTooltip();
sm.bindTooltip(sm._sapaTipHtml, {
direction: sm._sapaTipDir,
permanent: showSapaLabel,
offset: sm._sapaTipOffset,
opacity: 1.0,
className: "chain-sapa-tooltip"
});
}
}
function openPointByIndex(index) {
if (!pointMarkers[index]) {
return;
}
var marker = pointMarkers[index];
map.closePopup();
resetTroubleTooltips();
map.setView(marker.getLatLng(), Math.max(map.getZoom(), 13), { animate: true });
setTimeout(function () {
marker.openPopup();
}, 320);
}
function troubleTooltipAnchor(layer) {
if (!layer) {
return null;
}
// 線分上の中点(bounds中心だと海岸線で海側にずれる)
if (typeof layer.getCenter === "function" && layer._map) {
try {
return layer.getCenter();
} catch (err) {
// fall through
}
}
if (typeof layer.getBounds === "function") {
var b = layer.getBounds();
if (b && b.isValid()) {
return b.getCenter();
}
}
return null;
}
function findTroubleLayerByIndex(index) {
var sec = chainData.trouble && chainData.trouble[index];
if (!sec) {
return troubleLayers[index] || null;
}
for (var i = 0; i < troubleLayers.length; i++) {
var layer = troubleLayers[i];
if (!layer || !layer._chainTrouble) {
continue;
}
if (layer._chainTrouble === sec || layer._chainTrouble.name === sec.name) {
return layer;
}
}
return troubleLayers[index] || null;
}
// 区間全体が見えるようフィット(国道8号は北側=海側に余白を多め)
function fitTroubleSection(layer, options) {
if (!map || !layer || typeof layer.getBounds !== "function") {
return;
}
options = options || {};
var bounds = layer.getBounds();
if (!bounds || !bounds.isValid()) {
return;
}
var route = layer._chainTrouble ? String(layer._chainTrouble.route) : "";
var isMobile = window.matchMedia("(max-width: 640px)").matches;
var padLeft = Math.ceil(leftControlsWidth()) + (isMobile ? 12 : 16);
var padTop = isMobile ? 28 : 36;
var padRight = isMobile ? 28 : 48;
var padBottom = isMobile ? 36 : 52;
if (route === "8") {
// 海岸線が北側のため、上余白を増やして道路を画面中央寄りに
padTop = isMobile ? 72 : 100;
padBottom = isMobile ? 20 : 28;
}
map.invalidateSize();
map.fitBounds(bounds, {
paddingTopLeft: [padLeft, padTop],
paddingBottomRight: [padRight, padBottom],
maxZoom: options.maxZoom != null ? options.maxZoom : 13,
animate: options.animate !== false
});
}
function openTroubleByIndex(index) {
var layer = findTroubleLayerByIndex(index);
if (!map || !layer) {
return;
}
map.closePopup();
resetTroubleTooltips();
fitTroubleSection(layer, { maxZoom: 13, animate: true });
if (typeof layer.openTooltip === "function") {
setTimeout(function () {
var anchor = troubleTooltipAnchor(layer);
layer.openTooltip(anchor || undefined);
}, 300);
}
}
function getTroubleTooltipBindOptions(layer, permanent) {
var route = layer && layer._chainTrouble ? String(layer._chainTrouble.route) : "";
return {
sticky: !permanent,
permanent: !!permanent,
// 国道8号は海岸沿いのため、吹き出しを線の上側に出す
direction: route === "8" ? "top" : "auto",
opacity: 0.95,
className: "chain-trouble-tooltip"
};
}
function rebindTroubleTooltip(layer, permanent) {
if (!layer) {
return;
}
var content = "";
if (layer._chainTrouble) {
content = troubleTooltipHtml(layer._chainTrouble.name);
} else if (typeof layer.getTooltip === "function" && layer.getTooltip()) {
content = layer.getTooltip().getContent();
}
if (!content) {
return;
}
if (typeof layer.unbindTooltip === "function") {
layer.unbindTooltip();
}
layer.bindTooltip(content, getTroubleTooltipBindOptions(layer, permanent));
if (permanent && typeof layer.openTooltip === "function") {
var anchor = troubleTooltipAnchor(layer);
layer.openTooltip(anchor || undefined);
}
}
function resetTroubleTooltips() {
for (var i = 0; i < troubleLayers.length; i++) {
rebindTroubleTooltip(troubleLayers[i], false);
}
}
function openAllTroubleTooltips() {
for (var i = 0; i < troubleLayers.length; i++) {
rebindTroubleTooltip(troubleLayers[i], true);
}
}
function showAllTroubleSections() {
if (!map || !troubleLayers.length) {
return;
}
map.closePopup();
var bounds = null;
for (var i = 0; i < troubleLayers.length; i++) {
var layer = troubleLayers[i];
if (!layer || typeof layer.getBounds !== "function") {
continue;
}
var b = layer.getBounds();
if (!b || !b.isValid()) {
continue;
}
bounds = bounds ? bounds.extend(b) : L.latLngBounds(b);
}
if (!bounds || !bounds.isValid()) {
return;
}
map.fitBounds(bounds, {
padding: [40, 40],
maxZoom: 12,
animate: true
});
setTimeout(function () {
openAllTroubleTooltips();
}, 320);
}
function troubleSectionLabel(name) {
if (!name) {
return "";
}
var m = String(name).match(/((.+))/);
return m ? m[1] : name;
}
function troubleTooltipHtml(name) {
var section = troubleSectionLabel(name);
return '
' +
'
登坂不能車多発区間
' +
'
' + section + "
" +
"
";
}
function renderPointList() {
var body = document.getElementById("chain-point-list-body");
if (!body || !chainData.points) {
return;
}
function facilityIconsHtml(point) {
var icons = [];
for (var f = 0; f < FACILITIES.length; f++) {
var fac = FACILITIES[f];
if (point[fac.key]) {
icons.push(
'' +
'' +
""
);
} else {
icons.push('');
}
}
return '' + icons.join("") + "";
}
var col8 = [];
var col18 = [];
for (var i = 0; i < chainData.points.length; i++) {
var p = chainData.points[i];
var item =
'";
if (String(p.route) === "8") {
col8.push(item);
} else {
col18.push(item);
}
}
// 国道8号は北寄り(米山海岸)から表示
col8.reverse();
body.innerHTML =
'
' +
'
国道8号
' +
'
至新潟方面
' +
'
' + (col8.join("") || '
該当なし
') + "
" +
'
至富山方面
' +
"
" +
'
' +
'
国道18号
' +
'
至上越方面
' +
'
' + (col18.join("") || '
該当なし
') + "
" +
'
至長野方面
' +
"
";
$(body).off("click.chainlist").on("click.chainlist", ".chain-point-item", function () {
var idx = parseInt(this.getAttribute("data-index"), 10);
closeListModal(function () {
openPointByIndex(idx);
var mapEl = document.getElementById("map");
if (mapEl && mapEl.scrollIntoView) {
mapEl.scrollIntoView({ behavior: "smooth", block: "center" });
}
});
});
}
function renderTroubleList() {
var body = document.getElementById("chain-trouble-list-body");
if (!body || !chainData.trouble) {
return;
}
var col8 = [];
var col18 = [];
for (var i = 0; i < chainData.trouble.length; i++) {
var sec = chainData.trouble[i];
var label = troubleSectionLabel(sec.name);
var item =
'";
if (String(sec.route) === "8") {
col8.push(item);
} else {
col18.push(item);
}
}
// 国道8号は新潟方面(雁子浜側)から表示
col8.reverse();
body.innerHTML =
'