← All components

Isochrone

Reachability bands with an opacity ramp, from the isochrone API.

Previews run on MapScale's public Thailand archive.

Install

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

Usage

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>

Props

PropTypeDefaultDescription
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.

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" },
	})
}