500 lines
22 KiB
JavaScript
500 lines
22 KiB
JavaScript
/* ============================================================
|
|
Atroute — แผนที่เส้นทาง (Leaflet, vanilla)
|
|
------------------------------------------------------------
|
|
ข้อมูล:
|
|
<base>geojson-web/<route_id>.geojson เส้นทางจริงจาก QGIS (WGS84)
|
|
<base>busstop_list.json 154 ป้าย (มีพิกัด + buses-line)
|
|
<base>stations.json 19 สถานีของเรา (13 มีพิกัด)
|
|
<base>routes.json สาย ↔ สถานี
|
|
|
|
ธีม DESIGN.md §5: เส้นทาง --green-700 หนา 5px · หมุดสี่เหลี่ยมมน
|
|
ออฟไลน์: ยังวาดเส้นทาง/หมุดจากข้อมูลในเครื่องได้โดยไม่ต้องมี tile
|
|
|
|
API:
|
|
ATMap.route(el, '101') #/routes/:id
|
|
ATMap.forStop(el, 'at_main_terminal') #/stops/:code
|
|
ATMap.nearMe(button, resultEl) #/stops (ป้ายใกล้ฉัน)
|
|
ทุกตัวรับ opts = { base, stopHref } (สำหรับหน้า static นอก SPA)
|
|
============================================================ */
|
|
window.ATMap = (function () {
|
|
'use strict';
|
|
|
|
var DEF_BASE = '../resource/data/';
|
|
var TILES = 'https://tile.openstreetmap.org/{z}/{x}/{y}.png';
|
|
var ATTRIB = '© OpenStreetMap';
|
|
var NEAR_M = 40; /* ป้ายอยู่ "บนเส้นทาง" ถ้าใกล้ ≤ 40 ม. */
|
|
var STATION_NEAR_M = 80; /* สถานีอยู่ "ตามเส้นทาง" ถ้าใกล้ ≤ 80 ม. */
|
|
var LINE_W = 5;
|
|
var LINE_MAIN = '#14532d'; /* --green-700 */
|
|
var SHADES = ['#14532d', '#1f7a3d', '#2f8f5b', '#4a7c59', '#166534'];
|
|
var TH = { minLng: 97, maxLng: 103, minLat: 12, maxLat: 17.5 };
|
|
var cache = {};
|
|
|
|
/* ---------- พื้นฐาน ---------- */
|
|
function t(k, vars) { return window.I18N ? I18N.t(k, vars) : k; }
|
|
function esc(s) {
|
|
return String(s == null ? '' : s).replace(/&/g, '&').replace(/</g, '<')
|
|
.replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
|
|
}
|
|
function num(v) {
|
|
var n = typeof v === 'number' ? v : parseFloat(v);
|
|
return isFinite(n) ? n : null;
|
|
}
|
|
function pointOf(o) {
|
|
var lat = num(o && o.latitude), lng = num(o && o.longitude);
|
|
return (lat === null || lng === null) ? null : [lng, lat];
|
|
}
|
|
function url(o, path) { return ((o && o.base) || DEF_BASE) + path; }
|
|
function href(o, code) {
|
|
return (o && o.stopHref) ? o.stopHref(code) : '#/stops/' + encodeURIComponent(code);
|
|
}
|
|
function fetchJSON(u) {
|
|
return fetch(u, { cache: 'no-store' }).then(function (r) { return r.ok ? r.json() : null; }).catch(function () { return null; });
|
|
}
|
|
|
|
/* ---------- เรขาคณิต ---------- */
|
|
function hav(a, b) {
|
|
var R = 6371000, p1 = a[1] * Math.PI / 180, p2 = b[1] * Math.PI / 180;
|
|
var dp = p2 - p1, dl = (b[0] - a[0]) * Math.PI / 180;
|
|
var x = Math.sin(dp / 2) * Math.sin(dp / 2) + Math.cos(p1) * Math.cos(p2) * Math.sin(dl / 2) * Math.sin(dl / 2);
|
|
return 2 * R * Math.asin(Math.sqrt(x));
|
|
}
|
|
function segDist(p, a, b) {
|
|
var lat0 = (a[1] + b[1]) / 2 * Math.PI / 180, k = 111320;
|
|
var ax = a[0] * Math.cos(lat0) * k, ay = a[1] * k;
|
|
var bx = b[0] * Math.cos(lat0) * k, by = b[1] * k;
|
|
var px = p[0] * Math.cos(lat0) * k, py = p[1] * k;
|
|
var dx = bx - ax, dy = by - ay, L2 = dx * dx + dy * dy;
|
|
if (!L2) return { d: Math.hypot(px - ax, py - ay), t: 0 };
|
|
var u = Math.max(0, Math.min(1, ((px - ax) * dx + (py - ay) * dy) / L2));
|
|
return { d: Math.hypot(px - (ax + u * dx), py - (ay + u * dy)), t: u };
|
|
}
|
|
function linesOf(geom) {
|
|
if (!geom) return [];
|
|
var out = geom.type === 'LineString' ? [geom.coordinates] : (geom.coordinates || []);
|
|
return out.map(function (l) {
|
|
return l.filter(function (c) { return c[0] > TH.minLng && c[0] < TH.maxLng && c[1] > TH.minLat && c[1] < TH.maxLat; });
|
|
}).filter(function (l) { return l.length >= 2; });
|
|
}
|
|
function flatten(geo) {
|
|
return geo ? geo.features.reduce(function (a, f) { return a.concat(linesOf(f.geometry)); }, []) : [];
|
|
}
|
|
function lineLengthKm(lines) {
|
|
var m = 0;
|
|
lines.forEach(function (l) { for (var i = 1; i < l.length; i++) m += hav(l[i - 1], l[i]); });
|
|
return m / 1000;
|
|
}
|
|
function toLatLngs(lines) {
|
|
return lines.map(function (l) { return l.map(function (c) { return [c[1], c[0]]; }); });
|
|
}
|
|
/* ระยะสะสม (กม.) จากต้นทางถึงจุดที่ใกล้จุด p ที่สุด บนเส้นที่ยาวที่สุด */
|
|
function kmFromStart(mainLine, p) {
|
|
var best = { d: Infinity, km: 0 }, cum = 0;
|
|
for (var i = 1; i < mainLine.length; i++) {
|
|
var a = mainLine[i - 1], b = mainLine[i], seg = hav(a, b), r = segDist(p, a, b);
|
|
if (r.d < best.d) best = { d: r.d, km: (cum + r.t * seg) / 1000 };
|
|
cum += seg;
|
|
}
|
|
return best;
|
|
}
|
|
function longest(lines) {
|
|
return lines.slice().sort(function (a, b) {
|
|
var la = 0, lb = 0;
|
|
for (var i = 1; i < a.length; i++) la += hav(a[i - 1], a[i]);
|
|
for (var j = 1; j < b.length; j++) lb += hav(b[j - 1], b[j]);
|
|
return lb - la;
|
|
})[0];
|
|
}
|
|
|
|
/* ---------- โหลดข้อมูล (cache) ---------- */
|
|
function loadGeo(id, o) {
|
|
var key = 'geo:' + id;
|
|
if (cache[key] === undefined) cache[key] = fetchJSON(url(o, 'geojson-web/' + id + '.geojson'));
|
|
return Promise.resolve(cache[key]);
|
|
}
|
|
function loadJson(file, key, o) {
|
|
if (cache[key] === undefined) cache[key] = fetchJSON(url(o, file));
|
|
return Promise.resolve(cache[key]);
|
|
}
|
|
function stops(o) { return loadJson('busstop_list.json', 'stops', o).then(function (d) { return d || []; }); }
|
|
function stationList(o) { return loadJson('stations.json', 'stations', o).then(function (d) { return (d || {}).stations || []; }); }
|
|
|
|
/* ---------- องค์ประกอบแผนที่ ---------- */
|
|
function makeMap(el) {
|
|
var offline = navigator.onLine === false;
|
|
if (offline) el.classList.add('is-offline');
|
|
var map = L.map(el, { zoomControl: true, attributionControl: true, scrollWheelZoom: false });
|
|
map.setView([14.588, 100.455], 11);
|
|
if (!offline) L.tileLayer(TILES, { maxZoom: 18, minZoom: 8, attribution: ATTRIB }).addTo(map);
|
|
return { map: map, offline: offline };
|
|
}
|
|
function pinIcon(kind) {
|
|
return L.divIcon({ className: 'at-pin-wrap', html: '<span class="at-pin ' + kind + '"></span>', iconSize: [16, 16], iconAnchor: [8, 8] });
|
|
}
|
|
function legend(el, rows) {
|
|
var box = document.createElement('div');
|
|
box.className = 'at-legend';
|
|
rows.forEach(function (r) {
|
|
var d = document.createElement('div'), sw = document.createElement('i');
|
|
if (r.square) sw.className = 'sq';
|
|
sw.style.background = r.color;
|
|
d.appendChild(sw);
|
|
d.appendChild(document.createTextNode(r.label));
|
|
box.appendChild(d);
|
|
});
|
|
el.appendChild(box);
|
|
}
|
|
function caption(el, html) {
|
|
var c = el.parentNode && el.parentNode.querySelector('.at-map-caption');
|
|
if (c) c.innerHTML = html;
|
|
}
|
|
function fitAll(map, latlngs) {
|
|
var all = [];
|
|
latlngs.forEach(function (l) { all = all.concat(l); });
|
|
if (!all.length) return;
|
|
if (all.length === 1) { map.setView(all[0], 14); return; }
|
|
map.fitBounds(L.latLngBounds(all), { padding: [24, 24] });
|
|
}
|
|
function mapsLink(lat, lng) {
|
|
return 'https://www.google.com/maps/search/?api=1&query=' + lat + ',' + lng;
|
|
}
|
|
|
|
/* ป้ายหยุดรถ: hover = ข้อมูลย่อ · คลิก = popup (ประเภท/กม./ด้านทาง/สายที่ผ่าน + Google Maps) */
|
|
function drawShelters(map, hits, group) {
|
|
hits.forEach(function (h) {
|
|
var s = h.stop, pt = pointOf(s);
|
|
if (!pt) return;
|
|
var short = [];
|
|
if (s.kilometer) short.push('กม. ' + s.kilometer);
|
|
if (s.position) short.push(s.position);
|
|
if (s.type) short.push(s.type);
|
|
|
|
var html = '<div class="at-pop">';
|
|
html += '<b>' + esc(s.place || s.type || t('map.legendStop')) + '</b>';
|
|
var meta = [];
|
|
if (s.kilometer) meta.push('กม. ' + s.kilometer);
|
|
if (s.position) meta.push(s.position);
|
|
if (s.type && s.place) meta.push(s.type); /* ซ้ำกับหัวข้อเมื่อไม่มีชื่อสถานที่ */
|
|
if (meta.length) html += '<span>' + esc(meta.join(' · ')) + '</span>';
|
|
var bl = s['buses-line'] || [];
|
|
if (bl.length) html += '<span>' + esc(t('map.servedBy')) + ': <b>' + esc(bl.join(', ')) + '</b></span>';
|
|
html += '<a href="' + mapsLink(s.latitude, s.longitude) + '" target="_blank" rel="noopener">' + esc(t('map.openInMaps')) + '</a>';
|
|
html += '</div>';
|
|
|
|
L.marker([pt[1], pt[0]], { icon: pinIcon('stop') })
|
|
.bindTooltip(esc(short.join(' · ')), { direction: 'top', offset: [0, -8] })
|
|
.bindPopup(html, { closeButton: false, offset: [0, -6] })
|
|
.addTo(group);
|
|
});
|
|
}
|
|
|
|
/* สถานีของเรา: ชื่อ + คลิกไปหน้ารายละเอียดป้าย (opts.stopHref) */
|
|
function drawStations(map, hits, opts, group) {
|
|
hits.forEach(function (h) {
|
|
var s = h.station, pt = pointOf(s);
|
|
if (!pt) return;
|
|
var html = '<div class="at-pop"><b>' + esc(s.station_name_th) + '</b>';
|
|
html += '<span>' + esc([s.city_th, s.province_th].filter(Boolean).join(' · ')) + '</span>';
|
|
if (s.station_code) html += '<a href="' + esc(href(opts, s.station_code)) + '">' + esc(t('map.viewStopPage')) + '</a>';
|
|
html += '</div>';
|
|
L.marker([pt[1], pt[0]], { icon: pinIcon('term') })
|
|
.bindTooltip('<b>' + esc(s.station_name_th) + '</b><br>' + esc(s.province_th || ''), { direction: 'top', offset: [0, -9] })
|
|
.bindPopup(html, { closeButton: false, offset: [0, -6] })
|
|
.addTo(group);
|
|
});
|
|
}
|
|
|
|
function stationsNear(lines, maxM, o) {
|
|
return stationList(o).then(function (list) {
|
|
var hits = [];
|
|
list.forEach(function (s) {
|
|
var pt = pointOf(s);
|
|
if (!pt) return;
|
|
var best = Infinity;
|
|
for (var i = 0; i < lines.length && best > maxM; i++) {
|
|
var l = lines[i];
|
|
for (var j = 1; j < l.length; j++) {
|
|
var r = segDist(pt, l[j - 1], l[j]);
|
|
if (r.d < best) best = r.d;
|
|
}
|
|
}
|
|
if (best <= maxM) hits.push({ station: s, dist: best });
|
|
});
|
|
return hits;
|
|
});
|
|
}
|
|
function stopsNear(lines, o) {
|
|
return stops(o).then(function (list) {
|
|
var hits = [];
|
|
list.forEach(function (s) {
|
|
var pt = pointOf(s);
|
|
if (!pt) return;
|
|
var best = Infinity;
|
|
for (var i = 0; i < lines.length && best > NEAR_M; i++) {
|
|
var l = lines[i];
|
|
for (var j = 1; j < l.length; j++) {
|
|
var r = segDist(pt, l[j - 1], l[j]);
|
|
if (r.d < best) best = r.d;
|
|
}
|
|
}
|
|
if (best <= NEAR_M) hits.push({ stop: s, dist: Math.round(best) });
|
|
});
|
|
return hits;
|
|
});
|
|
}
|
|
|
|
/* รายการสถานีตามเส้นทาง พร้อม กม. จากต้นทาง */
|
|
function renderKmList(el, list) {
|
|
var host = document.getElementById(el);
|
|
if (!host) return;
|
|
host.innerHTML = '';
|
|
if (!list.length) return;
|
|
list.forEach(function (x) {
|
|
var li = document.createElement('li');
|
|
var a = document.createElement('a');
|
|
a.href = x.href;
|
|
a.className = 'at-km-row';
|
|
a.innerHTML = '<span class="at-km-dot" aria-hidden="true"></span>' +
|
|
'<span class="at-km-name">' + esc(x.name) + '</span>' +
|
|
'<span class="at-km-val">' + x.km + ' ' + esc(t('map.km')) + '</span>';
|
|
li.appendChild(a);
|
|
host.appendChild(li);
|
|
});
|
|
var wrap = host.closest('.at-km-wrap');
|
|
if (wrap) wrap.hidden = false;
|
|
}
|
|
|
|
/* ============================================================
|
|
#/routes/:id — เส้นทาง + สถานีตามเส้นทาง (พร้อม กม.) + ป้าย
|
|
============================================================ */
|
|
function route(el, routeId, opts) {
|
|
if (!el || !window.L) return;
|
|
opts = opts || {};
|
|
|
|
loadGeo(routeId, opts).then(function (geo) {
|
|
var lines = flatten(geo);
|
|
if (!lines.length) {
|
|
el.classList.add('is-offline');
|
|
caption(el, '<span class="at-offline">' + esc(t('map.empty')) + '</span>');
|
|
return;
|
|
}
|
|
|
|
var m = makeMap(el), map = m.map, group = L.layerGroup().addTo(map);
|
|
var latlngs = toLatLngs(lines);
|
|
|
|
latlngs.forEach(function (l) {
|
|
L.polyline(l, { color: LINE_MAIN, weight: LINE_W, opacity: 1, lineJoin: 'round', lineCap: 'round' }).addTo(group);
|
|
});
|
|
/* ต้นทาง/ปลายทาง */
|
|
[[latlngs[0][0], t('map.origin')], [latlngs[latlngs.length - 1][latlngs[latlngs.length - 1].length - 1], t('map.destination')]]
|
|
.forEach(function (e) {
|
|
L.marker(e[0], { icon: pinIcon('term') })
|
|
.bindTooltip(esc(e[1]), { direction: 'top', offset: [0, -8] }).addTo(group);
|
|
});
|
|
|
|
Promise.all([stopsNear(lines, opts), stationsNear(lines, STATION_NEAR_M, opts)]).then(function (res) {
|
|
var shelters = res[0], sts = res[1];
|
|
drawShelters(map, shelters, group);
|
|
drawStations(map, sts, opts, group);
|
|
fitAll(map, latlngs);
|
|
|
|
/* รายการสถานี + กม. จากต้นทาง (เรียงตามเส้นทาง) */
|
|
var main = longest(lines);
|
|
var rows = sts.map(function (h) {
|
|
var pt = pointOf(h.station);
|
|
var k = kmFromStart(main, pt);
|
|
return { name: h.station.station_name_th, km: k.km.toFixed(1), href: href(opts, h.station.station_code), _sort: k.km };
|
|
}).sort(function (a, b) { return a._sort - b._sort; });
|
|
renderKmList('route-map-list', rows);
|
|
|
|
var parts = ['<b>' + esc(routeId) + '</b> · ' + esc(t('map.captionRoute', { km: lineLengthKm(lines).toFixed(1), n: shelters.length }))];
|
|
if (sts.length) parts.push(esc(t('map.stations')) + ' <b>' + sts.length + '</b>');
|
|
if (m.offline) parts.push('<span class="at-offline">' + esc(t('map.offline')) + '</span>');
|
|
caption(el, parts.join(' · '));
|
|
});
|
|
|
|
legend(el, [
|
|
{ color: LINE_MAIN, label: t('map.legendRoute') },
|
|
{ color: '#1f7a3d', square: true, label: t('map.legendStop') },
|
|
{ color: '#f2a900', square: true, label: t('map.legendTerminal') }
|
|
]);
|
|
});
|
|
}
|
|
|
|
/* ============================================================
|
|
#/stops/:code — เส้นทางที่ผ่านป้ายนี้ + หมุดตัวป้าย
|
|
============================================================ */
|
|
function forStop(el, code, opts) {
|
|
if (!el || !window.L) return;
|
|
opts = opts || {};
|
|
|
|
Promise.all([stationList(opts), loadJson('routes.json', 'routes', opts)]).then(function (res) {
|
|
var list = res[0], routes = (res[1] || {}).routes || [];
|
|
var st = list.filter(function (s) { return s.station_code === code; })[0];
|
|
var serving = st ? routes.filter(function (r) { return (r.station_ids || []).indexOf(st.station_id) !== -1; }) : [];
|
|
var ids = serving.map(function (r) { return r.route_id; });
|
|
|
|
if (!st || !ids.length) {
|
|
el.classList.add('is-offline');
|
|
caption(el, '<span class="at-offline">' + esc(t(st ? 'map.noRoutesHere' : 'map.empty')) + '</span>');
|
|
return;
|
|
}
|
|
|
|
Promise.all(ids.map(function (id) { return loadGeo(id, opts); })).then(function (geos) {
|
|
var m = makeMap(el), map = m.map, group = L.layerGroup().addTo(map);
|
|
var all = [], allLines = [], legendRows = [], drawn = 0;
|
|
|
|
geos.forEach(function (g, idx) {
|
|
var lines = flatten(g);
|
|
if (!lines.length) return;
|
|
var color = SHADES[drawn % SHADES.length];
|
|
drawn++;
|
|
allLines = allLines.concat(lines);
|
|
toLatLngs(lines).forEach(function (l) {
|
|
L.polyline(l, { color: color, weight: LINE_W, opacity: 1, lineJoin: 'round', lineCap: 'round' }).addTo(group);
|
|
all.push(l);
|
|
});
|
|
legendRows.push({ color: color, label: t('map.route') + ' ' + ids[idx] });
|
|
});
|
|
|
|
if (!all.length) {
|
|
el.classList.add('is-offline');
|
|
caption(el, '<span class="at-offline">' + esc(t('map.empty')) + '</span>');
|
|
return;
|
|
}
|
|
|
|
stopsNear(allLines, opts).then(function (shelters) {
|
|
drawShelters(map, shelters, group);
|
|
var stPt = pointOf(st);
|
|
if (stPt) {
|
|
L.marker([stPt[1], stPt[0]], { icon: pinIcon('here'), zIndexOffset: 1000 })
|
|
.bindTooltip('<b>' + esc(st.station_name_th) + '</b><br>' + esc(t('map.thisStop')), { direction: 'top', offset: [0, -11] })
|
|
.addTo(group);
|
|
all.push([[stPt[1], stPt[0]]]);
|
|
}
|
|
fitAll(map, all);
|
|
|
|
var parts = [esc(t('map.captionStop', { r: drawn, n: shelters.length }))];
|
|
parts.push(stPt ? esc(t('map.stationPinned')) : esc(t('map.noStationPin')));
|
|
if (m.offline) parts.push('<span class="at-offline">' + esc(t('map.offline')) + '</span>');
|
|
caption(el, parts.join(' · '));
|
|
});
|
|
|
|
legendRows.push({ color: '#1f7a3d', square: true, label: t('map.legendStop') });
|
|
legendRows.push({ color: '#f2a900', square: true, label: t('map.legendHere') });
|
|
legend(el, legendRows);
|
|
});
|
|
});
|
|
}
|
|
|
|
/* ============================================================
|
|
ป้ายใกล้ฉัน — geolocation → ป้ายที่ใกล้ที่สุด + แผนที่
|
|
============================================================ */
|
|
function nearMe(button, resultEl, opts) {
|
|
if (!button || !resultEl) return;
|
|
opts = opts || {};
|
|
|
|
if (!navigator.geolocation) {
|
|
button.disabled = true;
|
|
resultEl.innerHTML = '<p class="at-near-msg">' + esc(t('map.noGeo')) + '</p>';
|
|
return;
|
|
}
|
|
|
|
button.addEventListener('click', function () {
|
|
button.disabled = true;
|
|
resultEl.hidden = false;
|
|
resultEl.innerHTML = '<p class="at-near-msg">' + esc(t('map.locating')) + '</p>';
|
|
|
|
navigator.geolocation.getCurrentPosition(function (pos) {
|
|
var me = [pos.coords.longitude, pos.coords.latitude];
|
|
Promise.all([stops(opts), stationList(opts)]).then(function (res) {
|
|
var items = [];
|
|
res[0].forEach(function (s) {
|
|
var pt = pointOf(s);
|
|
if (pt) items.push({ kind: 'stop', o: s, pt: pt, d: hav(me, pt) });
|
|
});
|
|
res[1].forEach(function (s) {
|
|
var pt = pointOf(s);
|
|
if (pt) items.push({ kind: 'station', o: s, pt: pt, d: hav(me, pt) });
|
|
});
|
|
items.sort(function (a, b) { return a.d - b.d; });
|
|
var near = items.slice(0, opts.limit || 8);
|
|
renderNear(resultEl, near, me, opts);
|
|
});
|
|
}, function () {
|
|
button.disabled = false;
|
|
resultEl.innerHTML = '<p class="at-near-msg at-near-err">' + esc(t('map.geoDenied')) + '</p>';
|
|
}, { enableHighAccuracy: true, timeout: 10000, maximumAge: 60000 });
|
|
});
|
|
}
|
|
|
|
function renderNear(host, near, me, opts) {
|
|
host.hidden = false;
|
|
host.innerHTML = '';
|
|
|
|
var box = document.createElement('div');
|
|
box.className = 'at-map';
|
|
box.id = 'near-me-map';
|
|
host.appendChild(box);
|
|
var cap = document.createElement('p');
|
|
cap.className = 'at-map-caption';
|
|
cap.textContent = t('map.nearestTitle');
|
|
host.appendChild(cap);
|
|
|
|
var ul = document.createElement('ul');
|
|
ul.className = 'at-near-list';
|
|
near.forEach(function (x) {
|
|
var li = document.createElement('li');
|
|
var name = x.kind === 'station' ? x.o.station_name_th : (x.o.place || x.o.type);
|
|
var meta = [];
|
|
if (x.kind === 'station') meta.push(t('map.legendTerminal'));
|
|
else {
|
|
if (x.o.kilometer) meta.push('กม. ' + x.o.kilometer);
|
|
if (x.o.position) meta.push(x.o.position);
|
|
if (x.o.type && x.o.place) meta.push(x.o.type);
|
|
}
|
|
var bl = x.o['buses-line'] || [];
|
|
if (bl.length) meta.push(t('map.servedBy') + ' ' + bl.join(', '));
|
|
|
|
var link = document.createElement('a');
|
|
link.className = 'at-near-row';
|
|
link.href = x.kind === 'station' ? href(opts, x.o.station_code) : mapsLink(x.o.latitude, x.o.longitude);
|
|
if (x.kind === 'stop') { link.target = '_blank'; link.rel = 'noopener'; }
|
|
link.innerHTML = '<span class="at-near-name">' + esc(name || t('map.legendStop')) + '</span>' +
|
|
'<span class="at-near-meta">' + esc(meta.join(' · ')) + '</span>' +
|
|
'<span class="at-near-dist">' + (x.d < 1000 ? Math.round(x.d) + ' ม.' : (x.d / 1000).toFixed(1) + ' ' + esc(t('map.km'))) + '</span>';
|
|
li.appendChild(link);
|
|
ul.appendChild(li);
|
|
});
|
|
host.appendChild(ul);
|
|
|
|
drawNearMap(box, near, me);
|
|
}
|
|
|
|
function drawNearMap(box, near, me) {
|
|
var offline = navigator.onLine === false;
|
|
var map = L.map(box, { zoomControl: true, attributionControl: true, scrollWheelZoom: false });
|
|
map.setView([me[1], me[0]], 13);
|
|
if (!offline) L.tileLayer(TILES, { maxZoom: 18, minZoom: 8, attribution: ATTRIB }).addTo(map);
|
|
else box.classList.add('is-offline');
|
|
|
|
L.marker([me[1], me[0]], { icon: L.divIcon({ className: 'at-pin-wrap', html: '<span class="at-pin here"></span>', iconSize: [18, 18], iconAnchor: [9, 9] }) })
|
|
.bindTooltip(esc(t('map.youAreHere')), { direction: 'top', offset: [0, -11] }).addTo(map);
|
|
|
|
var pts = [[me[1], me[0]]];
|
|
near.forEach(function (x) {
|
|
var kind = x.kind === 'station' ? 'term' : 'stop';
|
|
L.marker([x.pt[1], x.pt[0]], { icon: pinIcon(kind) })
|
|
.bindTooltip(esc(x.kind === 'station' ? x.o.station_name_th : (x.o.place || x.o.type)), { direction: 'top', offset: [0, -8] })
|
|
.addTo(map);
|
|
pts.push([x.pt[1], x.pt[0]]);
|
|
});
|
|
map.fitBounds(L.latLngBounds(pts), { padding: [28, 28] });
|
|
}
|
|
|
|
return {
|
|
route: route, forStop: forStop, nearMe: nearMe,
|
|
NEAR_M: NEAR_M, LINE_MAIN: LINE_MAIN
|
|
};
|
|
})();
|