กำลังโหลดตัวอย่างแผนที่...
ตัวอย่างการตรวจสอบพิกัดละติจูด/ลองจิจูดว่าอยู่ภายในขอบเขตประเทศไทยหรือไม่ ผ่าน /location/is-in-thailand API
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Thailand Boundary Check</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
href="https://unpkg.com/maplibre-gl@5.15.0/dist/maplibre-gl.css"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Kanit:wght@300;400;500;600&display=swap"
rel="stylesheet"
/>
<script src="https://unpkg.com/maplibre-gl@5.15.0/dist/maplibre-gl.js"></script>
<style>
body {
margin: 0;
font-family: "Kanit", system-ui, sans-serif;
height: 100vh;
width: 100vw;
overflow: hidden;
}
#map {
width: 100%;
height: 100%;
}
.search-container {
z-index: 10;
position: absolute;
top: 10px;
left: 10px;
background: white;
padding: 15px;
border-radius: 12px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
width: 320px;
display: flex;
flex-direction: column;
gap: 10px;
}
.input-group {
display: flex;
gap: 8px;
}
input {
font-family: "Kanit", system-ui, sans-serif;
flex: 1;
padding: 8px 12px;
border: 1px solid #ccc;
border-radius: 6px;
font-size: 14px;
}
#searchBtn {
font-family: "Kanit", system-ui, sans-serif;
background: #1677ff;
color: white;
border: none;
padding: 10px;
border-radius: 6px;
cursor: pointer;
font-weight: bold;
}
#searchBtn:hover {
background: #0958d9;
}
.status-box {
padding: 10px;
border-radius: 6px;
font-size: 13px;
text-align: center;
}
.in-th {
background: #f6ffed;
border: 1px solid #b7eb8f;
color: #389e0d;
}
.out-th {
background: #fff1f0;
border: 1px solid #ffa39e;
color: #cf1322;
}
.loading {
background: #e6f7ff;
border: 1px solid #91d5ff;
color: #1677ff;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<div class="search-container">
<div style="font-weight: bold; font-size: 14px">
ตรวจสอบพิกัด (ในไทยเท่านั้น)
</div>
<div class="input-group">
<input
id="lat"
placeholder="ละติจูด"
type="number"
step="any"
style="width: 100%"
/>
<input
id="lng"
placeholder="ลองจิจูด"
type="number"
step="any"
style="width: 100%"
/>
</div>
<button id="searchBtn">ตรวจสอบพิกัด</button>
<div id="statusArea" class="status-box hidden"></div>
</div>
<div id="map"></div>
<script>
const key = "your Api Key";
const map = new maplibregl.Map({
container: "map",
style: {
version: 8,
sources: {
osm: {
type: "raster",
tiles: [
`https://maptile.onegeo.co.th/tile/{z}/{x}/{y}.png?ApiKey=${key}`,
],
tileSize: 512,
},
},
layers: [{ id: "osm", type: "raster", source: "osm" }],
},
center: [100.5018, 13.7563],
zoom: 5,
});
map.addControl(new maplibregl.NavigationControl());
let currentMarker = null;
async function checkThailandBoundary(lat, lon) {
try {
const url = new URL(
"https://geoserver.ogs.co.th/location/is-in-thailand",
);
url.searchParams.set("lat", lat);
url.searchParams.set("lon", lon);
url.searchParams.set("ApiKey", key);
const res = await fetch(url);
const result = await res.json();
return parseInt(result.numberReturned) === 1;
} catch (e) {
return null;
}
}
const btn = document.getElementById("searchBtn");
const statusArea = document.getElementById("statusArea");
btn.addEventListener("click", async () => {
const lat = document.getElementById("lat").value;
const lon = document.getElementById("lng").value;
if (!lat || !lon) {
alert("กรุณากรอกพิกัด");
return;
}
statusArea.className = "status-box loading";
statusArea.innerHTML = "⏳ กำลังตรวจสอบ...";
statusArea.classList.remove("hidden");
if (currentMarker) currentMarker.remove();
const isInThailand = await checkThailandBoundary(lat, lon);
const finalLon = parseFloat(lon);
const finalLat = parseFloat(lat);
if (isInThailand === true) {
statusArea.className = "status-box in-th";
statusArea.innerHTML = "✅ อยู่ในประเทศไทย";
currentMarker = new maplibregl.Marker({ color: "#52c41a" })
.setLngLat([finalLon, finalLat])
.addTo(map);
map.flyTo({ center: [finalLon, finalLat], zoom: 12 });
} else {
statusArea.className = "status-box out-th";
statusArea.innerHTML = "❌ อยู่นอกประเทศไทย";
currentMarker = new maplibregl.Marker({ color: "#ff4d4f" })
.setLngLat([finalLon, finalLat])
.addTo(map);
map.flyTo({ center: [finalLon, finalLat], zoom: 8 });
}
});
</script>
</body>
</html>