-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
71 lines (59 loc) · 1.92 KB
/
route.ts
File metadata and controls
71 lines (59 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { NextRequest, NextResponse } from "next/server";
import type { PhotonReponse, AddressResult, PhotonFeature } from "@/lib/types";
function photonFeatureToAddressResult(feature: PhotonFeature): AddressResult {
const props = feature.properties;
const [lon, lat] = feature.geometry.coordinates;
const addressParts = [
props.housenumber && props.street
? `${props.housenumber} ${props.street}`
: (props.street ?? props.name),
props.city,
[props.state, props.postcode].filter(Boolean).join(" "),
].filter(Boolean);
const displayName = addressParts.join(", ");
return {
placeId: props.osm_id,
lat: lat.toString(),
lon: lon.toString(),
displayName,
address: {
housenumber: props.housenumber,
road: props.street,
city: props.city,
state: props.state,
postCode: props.postcode,
country: props.country,
countryCode: props.countrycode,
},
};
}
export async function GET(request: NextRequest) {
const query = request.nextUrl.searchParams.get("q");
if (!query || query.trim().length < 3) {
return NextResponse.json([]);
}
try {
// Philadelphia bounding box
const bbox = "-75.28,39.87,-75.0,40.14";
const params = new URLSearchParams({
q: query,
limit: "5", // maximum number of results returned
lang: "en",
bbox: bbox,
});
const response = await fetch(`https://photon.komoot.io/api/?${params.toString()}`, {
headers: {
Accept: "application/json",
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data: PhotonReponse = await response.json();
const results = data.features.map(photonFeatureToAddressResult);
return NextResponse.json(results);
} catch (error) {
console.error("Geocode API error:", error);
return NextResponse.json({ error: "Failed to fetch addresses" }, { status: 500 });
}
}