กำลังโหลดตัวอย่างแผนที่...
ตัวอย่างการค้นหาพิกัดจุดกึ่งกลาง (Centroid) จากรหัสตำบล อำเภอ หรือจังหวัด ด้วย /get-centroid API
<!doctype html>
<html lang="th">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>ค้นหาจุดศูนย์กลางจากรหัสพื้นที่ (MapLibre)</title>
<link
rel="stylesheet"
href="https://unpkg.com/maplibre-gl@5.6.0/dist/maplibre-gl.css"
/>
<script src="https://unpkg.com/maplibre-gl@5.6.0/dist/maplibre-gl.js"></script>
<style>
html,
body {
margin: 0;
height: 100%;
}
#map {
position: absolute;
inset: 0;
background: #eaf3f7;
}
#panel {
position: absolute;
top: 12px;
left: 12px;
z-index: 1;
min-width: 260px;
padding: 12px 14px;
background: rgba(255, 255, 255, 0.94);
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
font:
14px/1.5 "Segoe UI",
Tahoma,
sans-serif;
}
#panel h1 {
margin: 0 0 6px;
font-size: 15px;
}
#panel .hint {
color: #667;
font-size: 12px;
}
#form {
display: flex;
gap: 6px;
margin: 8px 0 6px;
}
#code {
flex: 1;
min-width: 0;
padding: 6px 8px;
border: 1px solid #b9c6d0;
border-radius: 6px;
font: inherit;
}
#code:focus {
outline: none;
border-color: #2f6f9f;
}
#go {
padding: 6px 14px;
border: 0;
border-radius: 6px;
background: #2f6f9f;
color: #fff;
font: inherit;
cursor: pointer;
}
#go:disabled {
background: #9db3c4;
cursor: default;
}
#status {
min-height: 21px;
font-size: 13px;
}
#status.error {
color: #a12626;
}
#status.ok {
color: #1d6b3a;
}
.maplibregl-popup-content {
font:
13px/1.6 "Segoe UI",
Tahoma,
sans-serif;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="panel">
<h1>ค้นหาจุดศูนย์กลางจากรหัสพื้นที่</h1>
<form id="form">
<input
id="code"
type="text"
inputmode="numeric"
autocomplete="off"
placeholder="เช่น 400101"
/>
<button id="go" type="submit">ค้นหา</button>
</form>
<div id="status"></div>
<span class="hint">
รหัส 2 หลัก = จังหวัด · 4 หลัก = อำเภอ · 6 หลัก = ตำบล
</span>
</div>
<script>
const API_URL = "https://geoserver.ogs.co.th/get-centroid";
const API_KEY = "your-api-key"; // ใส่ API Key ของคุณที่นี่
// ซูมปลายทางตามระดับของรหัส (ยิ่งรหัสยาว พื้นที่ยิ่งเล็ก)
const ZOOM_BY_LENGTH = { 2: 9, 4: 11, 6: 13 };
const map = new maplibregl.Map({
container: "map",
style: {
version: 8,
sources: {
basemap: {
type: "raster",
tiles: [
"https://maptile.onegeo.co.th/tile/{z}/{x}/{y}.png?ApiKey=" + API_KEY,
],
tileSize: 256,
attribution: "© OGS",
},
},
layers: [
{
id: "bg",
type: "background",
paint: { "background-color": "#eaf3f7" },
},
{ id: "basemap", type: "raster", source: "basemap" },
],
},
bounds: [
[97.343358, 5.613038],
[105.636964, 20.465074],
],
fitBoundsOptions: { padding: 30 },
maxZoom: 18,
});
map.addControl(new maplibregl.NavigationControl(), "top-right");
map.addControl(
new maplibregl.ScaleControl({ maxWidth: 120, unit: "metric" }),
);
const form = document.getElementById("form");
const codeInput = document.getElementById("code");
const goBtn = document.getElementById("go");
const statusEl = document.getElementById("status");
let marker = null;
function setStatus(text, kind) {
statusEl.textContent = text;
statusEl.className = kind || "";
}
// geometry ของ API เป็น null เสมอ พิกัดจริงอยู่ใน properties.lat/lon
// แต่เผื่อวันหลัง API ส่ง geometry มาด้วย จึงอ่านจาก geometry ก่อน
function coordsOf(feature) {
const g = feature.geometry;
if (g && g.type === "Point" && Array.isArray(g.coordinates)) {
return { lon: g.coordinates[0], lat: g.coordinates[1] };
}
const p = feature.properties || {};
const lat = Number(p.lat);
const lon = Number(p.lon);
return Number.isFinite(lat) && Number.isFinite(lon)
? { lat, lon }
: null;
}
// ชื่อ field ของรหัสต่างกันตามชั้นข้อมูล (pv_code / amp_code / tam_code)
function codeOf(feature) {
const p = feature.properties || {};
return p.tam_code || p.amp_code || p.pv_code || "-";
}
async function search(code) {
const url =
API_URL +
"?code=" +
encodeURIComponent(code) +
"&ApiKey=" +
encodeURIComponent(API_KEY);
const res = await fetch(url);
if (!res.ok)
throw new Error("เรียก API ไม่สำเร็จ (HTTP " + res.status + ")");
const data = await res.json();
const feature = data.features && data.features[0];
if (!feature) throw new Error("ไม่พบรหัส " + code);
const c = coordsOf(feature);
if (!c) throw new Error("ข้อมูลไม่มีพิกัดที่ใช้ได้");
return { coords: c, code: codeOf(feature), id: feature.id };
}
function showMarker(result) {
const { lat, lon } = result.coords;
if (marker) marker.remove();
marker = new maplibregl.Marker({ color: "#c0392b" })
.setLngLat([lon, lat])
.setPopup(
new maplibregl.Popup({ offset: 28 }).setHTML(
"<b>รหัส " +
result.code +
"</b><br>lat: " +
lat +
"<br>lon: " +
lon,
),
)
.addTo(map);
marker.togglePopup();
map.flyTo({
center: [lon, lat],
zoom: ZOOM_BY_LENGTH[result.code.length] || 13,
essential: true,
});
}
form.addEventListener("submit", async (e) => {
e.preventDefault();
const code = codeInput.value.trim();
if (!/^\d+$/.test(code)) {
setStatus("กรอกรหัสเป็นตัวเลขเท่านั้น", "error");
return;
}
if (!(code.length in ZOOM_BY_LENGTH)) {
setStatus("รหัสต้องยาว 2, 4 หรือ 6 หลัก", "error");
return;
}
goBtn.disabled = true;
setStatus("กำลังค้นหา…");
try {
const result = await search(code);
showMarker(result);
setStatus("พบตำแหน่งของรหัส " + result.code, "ok");
} catch (err) {
setStatus(err.message, "error");
} finally {
goBtn.disabled = false;
}
});
</script>
</body>
</html>