Spaces:
Runtime error
Runtime error
Commit
·
6b5fd4a
1
Parent(s):
66f237b
Make handleMapClick modular
Browse files- frontend/src/components/Map.js +59 -52
frontend/src/components/Map.js
CHANGED
|
@@ -23,8 +23,6 @@ L.Icon.Default.mergeOptions({
|
|
| 23 |
iconUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon.png',
|
| 24 |
shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-shadow.png',
|
| 25 |
});
|
| 26 |
-
// Add scale
|
| 27 |
-
// L.control.scale().addTo(window.Map);
|
| 28 |
|
| 29 |
const maxExplorationLimit = 50; // kilometers, the maximum amount user can select to explore.
|
| 30 |
|
|
@@ -223,7 +221,7 @@ const WikiMap = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSub
|
|
| 223 |
console.error("Error fetching Wikipedia content:", error);
|
| 224 |
}
|
| 225 |
}, [contentType]);
|
| 226 |
-
|
| 227 |
useEffect(() => {
|
| 228 |
if (searchQuery) {
|
| 229 |
fetchWiki(searchQuery);
|
|
@@ -239,49 +237,50 @@ const WikiMap = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSub
|
|
| 239 |
setWikiWidth(20);
|
| 240 |
};
|
| 241 |
|
| 242 |
-
const
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
} else {
|
| 278 |
-
console.error('Failed to fetch nearby pages');
|
| 279 |
-
}
|
| 280 |
-
} catch (err) {
|
| 281 |
-
console.error('Error fetching nearby pages:', err);
|
| 282 |
}
|
| 283 |
-
}
|
| 284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
if (updatedPoints.length > 2) {
|
| 286 |
updatedPoints.shift(); // keep only two
|
| 287 |
}
|
|
@@ -311,17 +310,26 @@ const WikiMap = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSub
|
|
| 311 |
setGeoDistance(null);
|
| 312 |
}
|
| 313 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
} else if (geoToolMode === "area") {
|
| 315 |
-
|
| 316 |
-
setAreaPoints(updated);
|
| 317 |
}
|
| 318 |
-
|
| 319 |
else {
|
| 320 |
-
// setMarkerPosition([lat, lon]);
|
| 321 |
console.log("Invalid tool mode:", geoToolMode);
|
| 322 |
}
|
| 323 |
|
| 324 |
-
}, [explorationMode,
|
| 325 |
|
| 326 |
useEffect(() => {
|
| 327 |
if (geoPoints.length === 2) {
|
|
@@ -374,7 +382,7 @@ const WikiMap = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSub
|
|
| 374 |
const {area, perimeter} = calculatePolygonArea(closed); // This took me a while to figure out, it should be just (lat, lon), not (lon, lat)
|
| 375 |
setPolygonArea(area);
|
| 376 |
setPolygonPerimeter(perimeter);
|
| 377 |
-
|
| 378 |
} else {
|
| 379 |
setPolygonArea(null);
|
| 380 |
setPolygonPerimeter(null);
|
|
@@ -433,7 +441,6 @@ const WikiMap = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSub
|
|
| 433 |
return lines;
|
| 434 |
};
|
| 435 |
|
| 436 |
-
// const longitudeLines = generateLongitudeLines(30, 1);
|
| 437 |
|
| 438 |
|
| 439 |
return (
|
|
|
|
| 23 |
iconUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon.png',
|
| 24 |
shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-shadow.png',
|
| 25 |
});
|
|
|
|
|
|
|
| 26 |
|
| 27 |
const maxExplorationLimit = 50; // kilometers, the maximum amount user can select to explore.
|
| 28 |
|
|
|
|
| 221 |
console.error("Error fetching Wikipedia content:", error);
|
| 222 |
}
|
| 223 |
}, [contentType]);
|
| 224 |
+
|
| 225 |
useEffect(() => {
|
| 226 |
if (searchQuery) {
|
| 227 |
fetchWiki(searchQuery);
|
|
|
|
| 237 |
setWikiWidth(20);
|
| 238 |
};
|
| 239 |
|
| 240 |
+
const handleExplorationClick = useCallback(async (lat, lon) => {
|
| 241 |
+
try{
|
| 242 |
+
const res = await fetch(`${BACKEND_URL}/wiki/nearby`, {
|
| 243 |
+
method: 'POST',
|
| 244 |
+
headers: { 'Content-Type': 'application/json' },
|
| 245 |
+
body: JSON.stringify({
|
| 246 |
+
lat: lat,
|
| 247 |
+
lon: lon,
|
| 248 |
+
radius: explorationRadius*1000,
|
| 249 |
+
limit: explorationLimit
|
| 250 |
+
}),
|
| 251 |
+
});
|
| 252 |
+
|
| 253 |
+
if (res.ok) {
|
| 254 |
+
const data = await res.json();
|
| 255 |
+
const markers = data.pages.map(page => ({
|
| 256 |
+
position: [page.lat, page.lon],
|
| 257 |
+
title: page.title,
|
| 258 |
+
distance: page.dist
|
| 259 |
+
}));
|
| 260 |
+
// setExplorationMarkers(markers);
|
| 261 |
+
// Now adding the main clicked point
|
| 262 |
+
setExplorationMarkers([
|
| 263 |
+
{
|
| 264 |
+
position: [lat, lon],
|
| 265 |
+
title: 'Clicked Location',
|
| 266 |
+
distance: 0,
|
| 267 |
+
isClickedPoint: true
|
| 268 |
+
},
|
| 269 |
+
...markers
|
| 270 |
+
]);
|
| 271 |
+
setShouldZoom(true);
|
| 272 |
+
console.log(`Found ${markers.length} nearby pages`); // Only backend results.
|
| 273 |
+
} else {
|
| 274 |
+
console.error('Failed to fetch nearby pages');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
}
|
| 276 |
+
} catch (err) {
|
| 277 |
+
console.error('Error fetching nearby pages:', err);
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
}, [explorationRadius, explorationLimit, setExplorationMarkers, setShouldZoom]);
|
| 281 |
+
|
| 282 |
+
const handleDistanceClick = useCallback(async (lat, lon) => {
|
| 283 |
+
const updatedPoints = [...geoPoints, { lat, lon }];
|
| 284 |
if (updatedPoints.length > 2) {
|
| 285 |
updatedPoints.shift(); // keep only two
|
| 286 |
}
|
|
|
|
| 310 |
setGeoDistance(null);
|
| 311 |
}
|
| 312 |
}
|
| 313 |
+
}, [geoPoints, geoUnit, setGeoPoints, setGeoDistance, setGeoSidebarOpen]);
|
| 314 |
+
|
| 315 |
+
const handleAreaClick = useCallback((lat, lon) => {
|
| 316 |
+
const updated = [...areaPoints, [lat, lon]];
|
| 317 |
+
setAreaPoints(updated);
|
| 318 |
+
}, [areaPoints, setAreaPoints]);
|
| 319 |
+
|
| 320 |
+
const handleMapClick = useCallback(async (lat, lon) => {
|
| 321 |
+
if (explorationMode) {
|
| 322 |
+
await handleExplorationClick(lat, lon);
|
| 323 |
+
} else if (geoToolMode === "distance") {
|
| 324 |
+
await handleDistanceClick(lat, lon);
|
| 325 |
} else if (geoToolMode === "area") {
|
| 326 |
+
handleAreaClick(lat, lon);
|
|
|
|
| 327 |
}
|
|
|
|
| 328 |
else {
|
|
|
|
| 329 |
console.log("Invalid tool mode:", geoToolMode);
|
| 330 |
}
|
| 331 |
|
| 332 |
+
}, [explorationMode, geoToolMode, handleExplorationClick, handleDistanceClick, handleAreaClick]);
|
| 333 |
|
| 334 |
useEffect(() => {
|
| 335 |
if (geoPoints.length === 2) {
|
|
|
|
| 382 |
const {area, perimeter} = calculatePolygonArea(closed); // This took me a while to figure out, it should be just (lat, lon), not (lon, lat)
|
| 383 |
setPolygonArea(area);
|
| 384 |
setPolygonPerimeter(perimeter);
|
| 385 |
+
|
| 386 |
} else {
|
| 387 |
setPolygonArea(null);
|
| 388 |
setPolygonPerimeter(null);
|
|
|
|
| 441 |
return lines;
|
| 442 |
};
|
| 443 |
|
|
|
|
| 444 |
|
| 445 |
|
| 446 |
return (
|