← คอมโพเนนต์ทั้งหมด

Isochrone

แถบพื้นที่เข้าถึงได้พร้อมระดับความโปร่งใส จาก isochrone API

ตัวอย่างทำงานบนคลังข้อมูลประเทศไทยสาธารณะของ MapScale

ติดตั้ง

npx shadcn@latest add https://mapscale.io/r/isochrone-layer.json

วิธีใช้

import { IsochroneLayer, useMapScaleIsochrone } from "@/components/mapscale/isochrone-layer"

const { isochrone, getIsochrone } = useMapScaleIsochrone()
useEffect(() => {
	void getIsochrone({ point: [100.5018, 13.7563], contours: [5, 10, 15] })
}, [getIsochrone])

<Map>
	<IsochroneLayer data={isochrone} />
</Map>

พร็อพ

พร็อพชนิดค่าเริ่มต้นคำอธิบาย
dataIsochroneCollection | null-FeatureCollection of polygons with a contour property.
colorstring--primaryFill color. Defaults to your --primary token.
minOpacitynumber0.12Opacity of the largest band.
maxOpacitynumber0.4Opacity of the smallest band.
fetcherMapScaleFetcher-useMapScaleIsochrone: replace the default call to /api/mapscale.

คีย์

แผนที่ การค้นหาที่อยู่ และรายละเอียดสถานที่ใช้คีย์ pk_ ที่ปลอดภัยบนเบราว์เซอร์ ส่วนเส้นทางและ isochrone ต้องใช้คีย์ลับ sk_ จึงต้องผ่านเส้นทางพร็อกซีบนเซิร์ฟเวอร์ของแอปคุณ

เส้นทางฝั่งเซิร์ฟเวอร์

ติดตั้งรายการ mapscale-proxy เพื่อเพิ่ม app/api/mapscale/[...path]/route.ts ซึ่งอ่าน MAPSCALE_SECRET_KEY บนเซิร์ฟเวอร์และส่งต่อคำขอเส้นทางและ isochrone

npx shadcn@latest add https://mapscale.io/r/mapscale-proxy.json
// app/api/mapscale/[...path]/route.ts
const UPSTREAM = "https://api.mapscale.io/v1"
const ALLOWED = new Set(["route", "isochrone"])

export async function GET(request: Request, { params }: { params: Promise<{ path: string[] }> }) {
	const name = (await params).path.join("/")
	const key = process.env.MAPSCALE_SECRET_KEY
	if (!ALLOWED.has(name)) return Response.json({ error: "Not found" }, { status: 404 })
	if (!key) return Response.json({ error: "MAPSCALE_SECRET_KEY is not set" }, { status: 500 })
	const upstream = await fetch(`${UPSTREAM}/${name}${new URL(request.url).search}`, {
		headers: { "X-API-Key": key },
	})
	return new Response(upstream.body, {
		status: upstream.status,
		headers: { "content-type": upstream.headers.get("content-type") ?? "application/json" },
	})
}