19
20 try {
21 const response = await fetch(
22 `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}¤t=temperature_2m,weather_code,wind_speed_10m,wind_direction_10m,relative_humidity_2m,surface_pressure,visibility,uv_index&hourly=temperature_2m,weather_code,precipitation,wind_speed_10m,wind_direction_10m,relative_humidity_2m&daily=temperature_2m_max,temperature_2m_min,weather_code,precipitation_sum,wind_speed_10m_max,wind_direction_10m_dominant,uv_index_max,sunrise,sunset&timezone=Europe/London`
23 );
68 } catch (error) {
69 console.error("Weather API error:", error);
70 return c.json({ error: "Failed to fetch weather data" }, 500);
71 }
72});
95 const weatherPromises = ukCities.map(async (city) => {
96 try {
97 const response = await fetch(
98 `https://api.open-meteo.com/v1/forecast?latitude=${city.lat}&longitude=${city.lon}¤t=temperature_2m,weather_code&timezone=Europe/London`
99 );
113 };
114 } catch (error) {
115 console.error(`Failed to fetch weather for ${city.name}:`, error);
116 return null;
117 }
124 } catch (error) {
125 console.error("Map weather API error:", error);
126 return c.json({ error: "Failed to fetch map weather data" }, 500);
127 }
128});
137 try {
138 // Use Open-Meteo geocoding API with focus on UK
139 const response = await fetch(
140 `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(query)}&count=10&language=en&format=json`
141 );
189 // Inject initial UK weather data to avoid extra round-trips
190 try {
191 const ukWeatherResponse = await fetch(
192 "https://api.open-meteo.com/v1/forecast?latitude=54.5&longitude=-2¤t=temperature_2m,weather_code,wind_speed_10m,wind_direction_10m,relative_humidity_2m,surface_pressure,visibility,uv_index&hourly=temperature_2m,weather_code,precipitation,wind_speed_10m,wind_direction_10m,relative_humidity_2m&daily=temperature_2m_max,temperature_2m_min,weather_code,precipitation_sum,wind_speed_10m_max,wind_direction_10m_dominant,uv_index_max,sunrise,sunset&timezone=Europe/London"
193 );
210 html = html.replace("</head>", `${dataScript}</head>`);
211 } catch (error) {
212 console.error("Failed to fetch initial weather data:", error);
213 }
214
216});
217
218export default app.fetch;
14
15 try {
16 const response = await fetch(
17 `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}¤t=temperature_2m,weather_code,wind_speed_10m,wind_direction_10m,relative_humidity_2m,surface_pressure,visibility,uv_index&hourly=temperature_2m,weather_code,precipitation,wind_speed_10m,wind_direction_10m,relative_humidity_2m&daily=temperature_2m_max,temperature_2m_min,weather_code,precipitation_sum,wind_speed_10m_max,wind_direction_10m_dominant,uv_index_max,sunrise,sunset&timezone=Europe/London`
18 );
63 } catch (error) {
64 console.error("Weather API error:", error);
65 return c.json({ error: "Failed to fetch weather data" }, 500);
66 }
67});
91 const weatherPromises = ukCities.map(async (city) => {
92 try {
93 const response = await fetch(
94 `https://api.open-meteo.com/v1/forecast?latitude=${city.lat}&longitude=${city.lon}¤t=temperature_2m,weather_code&timezone=Europe/London`
95 );
109 } as MapWeatherPoint;
110 } catch (error) {
111 console.error(`Failed to fetch weather for ${city.name}:`, error);
112 return null;
113 }
120 } catch (error) {
121 console.error("Map weather API error:", error);
122 return c.json({ error: "Failed to fetch map weather data" }, 500);
123 }
124});