กำลังโหลดตัวอย่างแผนที่...
ตัวอย่างการคลิกบนแผนที่เพื่อตรวจสอบข้อมูลขอบเขตและรหัสพื้นที่ ตำบล อำเภอ จังหวัด (GeoServer GetFeatureInfo /get-code)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Ogs Tile Service (MapLibre)</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="stylesheet"
href="https://unpkg.com/maplibre-gl@5.16.0/dist/maplibre-gl.css"
/>
<script src="https://unpkg.com/maplibre-gl@5.16.0/dist/maplibre-gl.js"></script>
<style>
body {
margin: 0;
padding: 0;
}
html,
body,
#map {
height: 100%;
}
:root {
--ink: #0f172a;
--muted: #94a3b8;
--line: #eef2f7;
--shadow:
0 8px 28px rgba(15, 23, 42, 0.14), 0 1px 2px rgba(15, 23, 42, 0.06);
}
#panel {
position: absolute;
top: 12px;
left: 12px;
z-index: 1;
background: #fff;
padding: 12px 14px;
border-radius: 14px;
box-shadow: var(--shadow);
font:
13px/1.55 system-ui,
-apple-system,
"Segoe UI",
sans-serif;
color: var(--ink);
max-width: 320px;
}
#panel code {
font-size: 12px;
color: #475569;
}
/* ---- popup: minimal, ขอบมน ---- */
.maplibregl-popup {
font:
13px/1.55 system-ui,
-apple-system,
"Segoe UI",
sans-serif;
color: var(--ink);
}
.maplibregl-popup-content {
padding: 14px 16px 12px;
border-radius: 16px;
box-shadow: var(--shadow);
background: #fff;
}
.maplibregl-popup-anchor-top .maplibregl-popup-tip,
.maplibregl-popup-anchor-top-left .maplibregl-popup-tip,
.maplibregl-popup-anchor-top-right .maplibregl-popup-tip {
border-bottom-color: #fff;
}
.maplibregl-popup-anchor-bottom .maplibregl-popup-tip,
.maplibregl-popup-anchor-bottom-left .maplibregl-popup-tip,
.maplibregl-popup-anchor-bottom-right .maplibregl-popup-tip {
border-top-color: #fff;
}
.maplibregl-popup-anchor-left .maplibregl-popup-tip {
border-right-color: #fff;
}
.maplibregl-popup-anchor-right .maplibregl-popup-tip {
border-left-color: #fff;
}
.maplibregl-popup-close-button {
top: 6px;
right: 6px;
width: 24px;
height: 24px;
border-radius: 50%;
color: var(--muted);
font-size: 16px;
line-height: 1;
background: transparent;
transition:
background 0.15s,
color 0.15s;
}
.maplibregl-popup-close-button:hover {
background: var(--line);
color: var(--ink);
}
.gfi-body {
max-height: 260px;
overflow: auto;
}
.gfi + .gfi {
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.gfi-title {
font-weight: 600;
letter-spacing: -0.01em;
padding-right: 22px;
margin-bottom: 8px;
}
.gfi-rows {
display: grid;
grid-template-columns: auto 1fr;
gap: 4px 14px;
margin: 0;
font-size: 12.5px;
}
.gfi-rows dt {
color: var(--muted);
white-space: nowrap;
}
.gfi-rows dd {
margin: 0;
overflow-wrap: anywhere;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="panel">
<b>GetFeatureInfo example</b><br />
คลิกบนแผนที่เพื่อตรวจสอบข้อมูลขอบเขตตำบล
</div>
<script>
const key = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
const API_KEY = "your api key";
// endpoint สำหรับสอบถามข้อมูลจากพิกัด (รับ lat/lon ตรง ๆ)
const GET_CODE_URL = "https://geoserver.ogs.co.th/get-code";
const map = new maplibregl.Map({
container: "map",
style: {
version: 8,
sources: {
"basemap-source": {
type: "raster",
tiles: ["https://maptile.onegeo.co.th/tile/{z}/{x}/{y}.png?ApiKey=API_KEY"],
tileSize: 256,
},
},
layers: [
{
id: "basemap-layer",
type: "raster",
source: "basemap-source",
paint: {},
},
],
},
zoom: 6,
center: [100.537997, 13.661845],
});
// วางปุ่ม zoom ไว้มุมขวาบน ไม่ให้ถูก #panel (มุมซ้ายบน) ทับ
map.addControl(
new maplibregl.NavigationControl({ showCompass: false }),
"top-right",
);
// ---- สอบถามข้อมูลจากพิกัดที่คลิก ----
// ส่ง lat/lon ไปยัง /get-code
function buildQueryUrl(lngLat) {
const params = {
ApiKey: API_KEY,
lat: lngLat.lat,
lon: lngLat.lng,
};
return GET_CODE_URL + "?" + new URLSearchParams(params).toString();
}
// /get-code ตอบกลับเป็น XML ServiceException แบบสุ่ม (ปัญหาฝั่งเซิร์ฟเวอร์)
// จึงต้องตรวจว่าเป็น JSON จริงก่อน parse แล้วลองใหม่อัตโนมัติ
const MAX_ATTEMPTS = 4;
function readServiceException(text) {
const doc = new DOMParser().parseFromString(text, "text/xml");
const el = doc.getElementsByTagName("ServiceException")[0];
return el ? el.textContent.trim() : text.slice(0, 200);
}
async function queryPoint(url) {
let lastError;
for (let i = 1; i <= MAX_ATTEMPTS; i++) {
const res = await fetch(url);
const text = await res.text();
if (!res.ok) throw new Error("HTTP " + res.status);
if (text.trimStart().startsWith("{")) return JSON.parse(text);
lastError = new Error(readServiceException(text));
console.warn(
"get-code ตอบกลับไม่ใช่ JSON (ครั้งที่ " + i + "):",
lastError.message,
);
}
throw lastError;
}
function featureTitle(f) {
const p = f.properties || {};
return (
p.tam_name || p.amp_name || p.pv_name || p.name || f.id || "feature"
);
}
function renderFeatures(features) {
const items = features
.map((f) => {
const rows = Object.entries(f.properties || {})
.map(
([k, v]) =>
"<dt>" +
k +
"</dt><dd>" +
(v === null ? "—" : String(v)) +
"</dd>",
)
.join("");
return (
'<div class="gfi"><div class="gfi-title">' +
featureTitle(f) +
'</div><dl class="gfi-rows">' +
rows +
"</dl></div>"
);
})
.join("");
return '<div class="gfi-body">' + items + "</div>";
}
const popup = new maplibregl.Popup({ maxWidth: "340px" });
let requestId = 0;
map.on("click", async (e) => {
const url = buildQueryUrl(e.lngLat);
console.log("get-code:", url);
// ปิด popup เดิมทันที และกันผลลัพธ์ของคลิกเก่าที่มาถึงช้ากว่า
const id = ++requestId;
popup.remove();
try {
const data = await queryPoint(url);
if (id !== requestId) return;
const features = data.features || [];
// ไม่มีข้อมูล ณ ตำแหน่งนี้ → ไม่ต้องแสดง popup
if (features.length === 0) return;
popup
.setLngLat(e.lngLat)
.setHTML(renderFeatures(features))
.addTo(map);
} catch (err) {
if (id !== requestId) return;
popup
.setLngLat(e.lngLat)
.setHTML("เกิดข้อผิดพลาด: " + err.message)
.addTo(map);
}
});
// raster layer ไม่รองรับ mouse event รายฟีเจอร์ จึงตั้ง cursor ทั้งแคนวาส
map.on("load", () => {
map.getCanvas().style.cursor = "crosshair";
});
</script>
</body>
</html>