กำลังโหลดตัวอย่างแผนที่...
ตัวอย่างการซ้อนทับชั้นข้อมูล Web Map Service (OGS:Province_TH) จาก GeoServer ลงบนแผนที่ MapLibre GL JS และ Leaflet
<!doctype html>
<html lang="th">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>OGS WMS - Maplibre</title>
<link
href="https://unpkg.com/maplibre-gl@5.15.0/dist/maplibre-gl.css"
rel="stylesheet"
/>
<script src="https://unpkg.com/maplibre-gl@5.15.0/dist/maplibre-gl.js"></script>
<style>
body {
margin: 0;
padding: 0;
height: 100vh;
width: 100vw;
overflow: hidden;
font-family: system-ui, sans-serif;
}
#map {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
const apiKey = "yourApiKey";
// 1. เริ่มต้นสร้างแผนที่ฐาน (OSM)
const map = new maplibregl.Map({
container: "map",
style: {
version: 8,
sources: {
base: {
type: "raster",
tiles: [
"https://maptile.onegeo.co.th/tile/{z}/{x}/{y}.png?ApiKey=" + apiKey,
],
tileSize: 256,
},
},
layers: [{ id: "base-layer", type: "raster", source: "base" }],
},
center: [100.5018, 13.7563],
zoom: 6,
});
map.addControl(new maplibregl.NavigationControl());
function addFixedWMSLayer() {
const config = {
service: "WMS",
request: "GetMap",
layers: "OGS:Province_TH",
styles: "green_color",
format: "image/png",
transparent: true,
srs: "EPSG:3857",
width: 256,
height: 256,
};
const wmsUrl =
"https://geoserver.onegeo.co.th/geoserver/OGS/wms" +
"?SERVICE=" + config.service +
"&REQUEST=" + config.request +
"&LAYERS=" + config.layers +
"&STYLES=" + config.styles +
"&FORMAT=" + config.format +
"&TRANSPARENT=" + config.transparent +
"&SRS=" + config.srs +
"&WIDTH=" + config.width +
"&HEIGHT=" + config.height +
"&BBOX={bbox-epsg-3857}" +
"&ApiKey=" + apiKey;
map.addSource("ogs-wms-source", {
type: "raster",
tiles: [wmsUrl],
tileSize: 256,
});
map.addLayer({
id: "ogs-wms-layer",
type: "raster",
source: "ogs-wms-source",
paint: { "raster-opacity": 0.8 },
});
}
map.on("load", () => {
addFixedWMSLayer();
});
</script>
</body>
</html>