← All components
Route
Draw a route and request one through your own server.
Previews run on MapScale's public Thailand archive.
Install
npx shadcn@latest add https://mapscale.io/r/route-layer.jsonUsage
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>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| 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. |
Keys
Maps, autocomplete and place details use a publishable pk_ key that is safe in the browser. Routing and isochrones need a secret sk_ key, so they go through the proxy route your app serves.
Server route
Install the mapscale-proxy item to add app/api/mapscale/[...path]/route.ts. It reads MAPSCALE_SECRET_KEY on the server and forwards route and isochrone calls.
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" },
})
}