← คอมโพเนนต์ทั้งหมด
เส้นทาง
วาดเส้นทางและขอเส้นทางผ่านเซิร์ฟเวอร์ของคุณเอง
ตัวอย่างทำงานบนคลังข้อมูลประเทศไทยสาธารณะของ MapScale
ติดตั้ง
npx shadcn@latest add https://mapscale.io/r/route-layer.jsonวิธีใช้
import { RouteLayer, useMapScaleRoute } from "@/components/mapscale/route-layer"
const { route, getRoute } = useMapScaleRoute()
useEffect(() => {
void getRoute({ coordinates: [[100.5018, 13.7563], [100.5685, 13.7367]] })
}, [getRoute])
<Map>
<RouteLayer data={route} />
</Map>พร็อพ
| พร็อพ | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| data | LineString | Feature | FeatureCollection | null | - | A LineString, Feature or FeatureCollection. Null draws nothing. |
| color | string | --primary | Line color. Defaults to your --primary token. |
| width | number | 5 | Line width in pixels. |
| fetcher | MapScaleFetcher | - | useMapScaleRoute: 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" },
})
}