เอกสาร MapScale
การแสดงผลข้อมูลบนแผนที่

วงรอบเวลาเดินทางด้วย isochrone

วาดวงรอบเวลาขับรถที่ซ้อนกันรอบจุดหนึ่ง โปร่งแสงและเปิดปิดได้ จากไฟล์หรือจาก isochrone API ของ MapScale

ปานกลาง

สิ่งที่คุณจะสร้าง

วงรอบเวลาขับรถที่ซ้อนกัน (8, 15, 23 และ 30 นาที) รอบอโศกในกรุงเทพฯ บนแผนที่พื้นหลังสีเข้ม พร้อม legend ที่เปิดปิดแต่ละวงได้ isochrone คือ polygon ของทุกที่ที่เดินทางไปถึงได้ภายใน N นาที ตัวอย่างอ่านผลลัพธ์ Valhalla ที่เก็บไว้ใน data/<minutes>.json จึงต้องเปิดผ่าน http (fetch ใช้จาก file:// ไม่ได้) ขั้นตอนสุดท้ายสลับไปใช้ API จริง ข้อมูลพื้นฐานของแผนที่เป็น © OpenStreetMap contributors

สิ่งที่คุณจะสร้าง
cd docs/mapscale/visualizations/isochrones
python3 -m http.server 4102
# open http://localhost:4102/

สร้างหน้าเว็บและแผนที่พื้นหลังสีเข้ม

ใช้ basemap แบบ pmtiles:// ที่ไม่ต้องใช้ key เหมือนเดิม ตัดเหลือพื้นที่ น้ำ และถนน เพื่อให้วงรอบเป็นจุดเด่น legend เป็น div ธรรมดาที่ขั้นตอนที่ 5 จะเติมเนื้อหา attribution ของ source ใส่เครดิต OpenStreetMap ไว้แล้ว

สร้างหน้าเว็บและแผนที่พื้นหลังสีเข้ม
<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	<title>Travel-time rings</title>
	<style>
		html,
		body,
		#map {
			height: 100%;
			margin: 0;
		}
	</style>
	<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.css" />
	<script src="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.js"></script>
	<script src="https://unpkg.com/pmtiles@4/dist/pmtiles.js"></script>
</head>
<body>
	<div id="map"></div>
	<div id="legend" style="position:absolute;top:12px;left:12px;z-index:1;background:rgba(10,13,18,0.85);color:#cfe7ff;font:12px/1.6 ui-monospace,monospace;padding:8px 10px;border-radius:6px"><b>Drive time</b></div>
	<script>
		maplibregl.addProtocol("pmtiles", new pmtiles.Protocol().tile)

		const map = new maplibregl.Map({
			container: "map",
			center: [100.5602, 13.7373], // Asok junction, Bangkok
			zoom: 10.4,
			minZoom: 4,
			style: {
				version: 8,
				glyphs: "https://maps.thaipropertiesnetwork.com/{fontstack}/{range}.pbf",
				sources: {
					ms: {
						type: "vector",
						url: "pmtiles://https://maps.thaipropertiesnetwork.com/thailand-v2.pmtiles",
						attribution: "© OpenStreetMap contributors · MapScale",
					},
				},
				layers: [
					{ id: "background", type: "background", paint: { "background-color": "#0a0d12" } },
					{ id: "landuse", type: "fill", source: "ms", "source-layer": "landuse", paint: { "fill-color": "#0e1219" } },
					{ id: "water", type: "fill", source: "ms", "source-layer": "water", paint: { "fill-color": "#0c141f" } },
					{
						id: "roads",
						type: "line",
						source: "ms",
						"source-layer": "transportation",
						filter: ["==", "$type", "LineString"],
						paint: { "line-color": "#1b232e", "line-width": { base: 1.2, stops: [[11, 0.5], [18, 10]] } },
					},
				],
			},
		})

		map.on("load", async () => {
			// Steps 3 to 5 go here.
		})
	</script>
</body>
</html>

โหลด polygon ของ isochrone

แต่ละไฟล์ในตัวอย่างเป็น GeoJSON FeatureCollection ที่มี polygon หนึ่งรูป และ properties เป็น { contour: <นาที>, metric: "time" } รวมทุกไฟล์เป็น collection เดียว แล้วเรียงจาก contour ที่ใหญ่ที่สุดก่อน layer ที่เพิ่มทีหลังจะวาดทับ ดังนั้นการเรียงนี้ทำให้วงเล็กอยู่เหนือวงใหญ่

โหลด polygon ของ isochrone
const ORIGIN = [100.5602, 13.7373]
const MINUTES = [8, 15, 23, 30]
// warm (close) -> cool (far), one colour per contour
const RAMP = { 8: "#ffb454", 15: "#ff6b81", 23: "#a06cff", 30: "#35b7ff" }

const files = await Promise.all(MINUTES.map((m) => fetch("./data/" + m + ".json").then((r) => r.json())))
const features = files
	.flatMap((fc) => fc.features)
	.sort((a, b) => b.properties.contour - a.properties.contour)

map.addSource("iso", { type: "geojson", data: { type: "FeatureCollection", features } })

วาด fill และเส้นขอบโปร่งแสงให้แต่ละวง

แต่ละ contour มี fill layer หนึ่งและ line layer หนึ่ง กรองด้วยค่า contour ของตัวเอง เรียงตามลำดับที่เรียงไว้ ความทึบต่ำทำให้เห็นแผนที่พื้นหลังและวงที่ซ้อนกัน ได้ลุคคล้าย heat map

วาด fill และเส้นขอบโปร่งแสงให้แต่ละวง
for (const f of features) {
	const m = f.properties.contour
	map.addLayer({
		id: "iso-fill-" + m,
		type: "fill",
		source: "iso",
		filter: ["==", ["get", "contour"], m],
		paint: { "fill-color": RAMP[m], "fill-opacity": 0.28 },
	})
	map.addLayer({
		id: "iso-line-" + m,
		type: "line",
		source: "iso",
		filter: ["==", ["get", "contour"], m],
		paint: { "line-color": RAMP[m], "line-width": 1.5, "line-opacity": 0.95 },
	})
}

เพิ่มจุดเริ่มต้น legend และปรับกล้อง

marker แสดงจุดเริ่มต้น checkbox ของแต่ละวงเปิดปิดสอง layer ของวงนั้น และ fitBounds ปรับกล้องให้พอดีกับวงที่ใหญ่ที่สุด geometry ของวงอาจซ้อนหลายชั้น การคำนวณ bounds จึงวนลงไปจนถึงคู่พิกัด

เพิ่มจุดเริ่มต้น legend และปรับกล้อง
new maplibregl.Marker({ color: "#ffffff" }).setLngLat(ORIGIN).addTo(map)

const legend = document.getElementById("legend")
for (const m of MINUTES) {
	const label = document.createElement("label")
	label.style.display = "block"
	label.innerHTML = '<input type="checkbox" checked /> ' + m + " min"
	label.style.color = RAMP[m]
	label.querySelector("input").addEventListener("change", (e) => {
		const v = e.target.checked ? "visible" : "none"
		map.setLayoutProperty("iso-fill-" + m, "visibility", v)
		map.setLayoutProperty("iso-line-" + m, "visibility", v)
	})
	legend.appendChild(label)
}

// features[0] is the biggest ring (sorted largest first)
const bounds = new maplibregl.LngLatBounds()
const walk = (c) => (typeof c[0] === "number" ? bounds.extend(c) : c.forEach(walk))
walk(features[0].geometry.coordinates)
map.fitBounds(bounds, { padding: 40, animate: false })

ใช้ API จริงและไปต่อที่ไหนดี

แทนที่จะใช้ไฟล์ ให้ขอ isochrone จาก API ของ MapScale ได้ทุกที่ในประเทศไทย ผลลัพธ์เป็น FeatureCollection แต่ละ feature มี properties { contour, metric } โดย metric เป็น "time" (นาที) หรือ "distance" (กม.) และ geometry เป็น Polygon หรือ MultiPolygon ค่า contours ต้องเป็นนาทีทั้งหมด (10,20,30) หรือกิโลเมตรทั้งหมด (1km,2km) ถ้าผสมกันจะได้ 400 INVALID_CONTOURS ใน browser ให้ใช้ publishable key นำ loop เรียงและสร้าง layer ข้างบนกลับมาใช้ แล้วปรับ MINUTES และ RAMP ให้ตรงกับ contours ที่ขอ หน้าเว็บฉบับเต็มที่ทดสอบแล้วคือ docs/mapscale/visualizations/isochrones

ใช้ API จริงและไปต่อที่ไหนดี
async function loadFromApi([lng, lat]) {
	const url =
		"https://api.mapscale.io/v1/isochrone?point=" + lng + "," + lat +
		"&contours=10,20,30&key=pk_test_your_key_here"
	const res = await fetch(url)
	if (!res.ok) throw new Error("isochrone request failed")
	const fc = await res.json()
	// Sort by properties.contour descending, then add the layers as above.
	return fc.features.sort((a, b) => b.properties.contour - a.properties.contour)
}

On this page

ตั้งค่า Analytics