21 }
22
23 const apiKey = Deno.env.get("GOOGLE_MAPS_API_KEY");
24 if (!apiKey) {
25 return new Response(JSON.stringify({ error: "API key is not configured" }), {
26 status: 500,
27 headers: { "Content-Type": "application/json" },
35 const gyms = await blob.getJSON("SF_Gyms");
36
37 const nearestGrocery = await findNearest(origin, groceries, apiKey);
38 const nearestGym = await findNearest(origin, gyms, apiKey);
39
40 const fidiDestination = "548 Market St, San Francisco, CA 94104";
41 const fidiDrivingTime = await getDrivingTime(origin, fidiDestination, apiKey);
42
43 const robloxDestination = "910 Park Pl Ste 300, San Mateo, CA 94403";
44 const robloxDrivingTime = await getDrivingTime(origin, robloxDestination, apiKey, "09:00:00", "Tuesday");
45
46 const samsaraDestination = "1 De Haro St, San Francisco, CA 94103";
47 const samsaraTransitTime = await getTransitTime(origin, samsaraDestination, apiKey);
48
49 const zipCode = await getZipCode(origin, apiKey);
50 const neighborhoodZipMap = await blob.getJSON("SF_Neighborhood_ZIP");
51 const neighborhood = neighborhoodZipMap[zipCode] || "Unknown";
72}
73
74async function findNearest(origin: string, locations: any[], apiKey: string): Promise<any> {
75 const batchSize = 25;
76 let nearestLocation = null;
80 const batch = locations.slice(i, i + batchSize);
81 const destinations = batch.map(location => `${location.gps.lat},${location.gps.lng}`).join('|');
82 const distanceMatrixUrl = `https://maps.googleapis.com/maps/api/distancematrix/json?origins=${encodeURIComponent(origin)}&destinations=${encodeURIComponent(destinations)}&mode=driving&key=${apiKey}`;
83
84 const response = await fetch(distanceMatrixUrl);
86
87 if (data.status !== "OK") {
88 throw new Error(`Distance Matrix API failed. Status: ${data.status}`);
89 }
90
108}
109
110async function getDrivingTime(origin: string, destination: string, apiKey: string, arrivalTime?: string, arrivalDay?: string): Promise<string> {
111 let directionsUrl = `https://maps.googleapis.com/maps/api/directions/json?origin=${encodeURIComponent(origin)}&destination=${encodeURIComponent(destination)}&mode=driving&key=${apiKey}`;
112
113 if (arrivalTime && arrivalDay) {
129}
130
131async function getTransitTime(origin: string, destination: string, apiKey: string): Promise<string> {
132 const directionsUrl = `https://maps.googleapis.com/maps/api/directions/json?origin=${encodeURIComponent(origin)}&destination=${encodeURIComponent(destination)}&mode=transit&key=${apiKey}`;
133
134 const directionsResponse = await fetch(directionsUrl);
144}
145
146async function getZipCode(address: string, apiKey: string): Promise<string> {
147 const geocodeUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(address)}&key=${apiKey}`;
148 const response = await fetch(geocodeUrl);
149 const data = await response.json();