Spaces:
Runtime error
Runtime error
Commit
·
8b80ab0
1
Parent(s):
2b18158
Make distance text on sidebar and polyline continiously change during dragging
Browse filesAs the distance marker gets dragged, every 100ms, the distance will get updated. Doing it instantaneously does too many backend calls, thus added a timeout,
frontend/src/components/Map.js
CHANGED
@@ -214,7 +214,7 @@ const Map = ( { onMapClick, searchQuery, contentType } ) => {
|
|
214 |
const cacheKey = `${geoPoints[0].lat},${geoPoints[0].lon}-${geoPoints[1].lat},${geoPoints[1].lon}-${geoUnit}`;
|
215 |
if (distanceCache.current[cacheKey]) {
|
216 |
setGeoDistance(distanceCache.current[cacheKey]);
|
217 |
-
console.log("Using cached distance:", distanceCache.current[cacheKey]);
|
218 |
return;
|
219 |
}
|
220 |
|
@@ -234,16 +234,24 @@ const Map = ( { onMapClick, searchQuery, contentType } ) => {
|
|
234 |
const data = await res.json();
|
235 |
setGeoDistance(data.distance);
|
236 |
distanceCache.current[cacheKey] = data.distance; // Setting up the cache here, forgot it in first attempt.
|
237 |
-
console.log("
|
238 |
}
|
239 |
catch (err) {
|
240 |
console.error('Failed to fetch distance:', err);
|
241 |
setGeoDistance(null);
|
242 |
}
|
243 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
fetchDistance();
|
245 |
}
|
246 |
-
}, [geoPoints, geoUnit]);
|
247 |
|
248 |
return (
|
249 |
<div ref={containerRef} style={{ display: 'flex', height: '100vh', width: '100%', overflow: 'hidden' }}>
|
@@ -350,6 +358,12 @@ const Map = ( { onMapClick, searchQuery, contentType } ) => {
|
|
350 |
dragstart: () => {
|
351 |
setIsGeoMarkerDragging(true);
|
352 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
353 |
dragend: (e) => {
|
354 |
const { lat, lng } = e.target.getLatLng();
|
355 |
const updated = [...geoPoints];
|
@@ -366,7 +380,7 @@ const Map = ( { onMapClick, searchQuery, contentType } ) => {
|
|
366 |
))}
|
367 |
|
368 |
{/* Polyline if 2 points are selected and sidebar is open */}
|
369 |
-
{geoSidebarOpen && geoPoints.length === 2 &&
|
370 |
<Polyline
|
371 |
key={geoPoints.map(pt => `${pt.lat},${pt.lon}`).join('-')}
|
372 |
positions={generateGeodesicPoints(
|
|
|
214 |
const cacheKey = `${geoPoints[0].lat},${geoPoints[0].lon}-${geoPoints[1].lat},${geoPoints[1].lon}-${geoUnit}`;
|
215 |
if (distanceCache.current[cacheKey]) {
|
216 |
setGeoDistance(distanceCache.current[cacheKey]);
|
217 |
+
console.log("Using cached distance:", distanceCache.current[cacheKey].toFixed(3));
|
218 |
return;
|
219 |
}
|
220 |
|
|
|
234 |
const data = await res.json();
|
235 |
setGeoDistance(data.distance);
|
236 |
distanceCache.current[cacheKey] = data.distance; // Setting up the cache here, forgot it in first attempt.
|
237 |
+
console.log("Using normal distance method:", data.distance.toFixed(3));
|
238 |
}
|
239 |
catch (err) {
|
240 |
console.error('Failed to fetch distance:', err);
|
241 |
setGeoDistance(null);
|
242 |
}
|
243 |
};
|
244 |
+
|
245 |
+
if (isGeoMarkerDragging){
|
246 |
+
const timeoutId = setTimeout(() => {
|
247 |
+
fetchDistance();
|
248 |
+
}, 100); // 100ms timeout, before every backend call during dragging
|
249 |
+
return () => clearTimeout(timeoutId);
|
250 |
+
}
|
251 |
+
|
252 |
fetchDistance();
|
253 |
}
|
254 |
+
}, [geoPoints, geoUnit, isGeoMarkerDragging]);
|
255 |
|
256 |
return (
|
257 |
<div ref={containerRef} style={{ display: 'flex', height: '100vh', width: '100%', overflow: 'hidden' }}>
|
|
|
358 |
dragstart: () => {
|
359 |
setIsGeoMarkerDragging(true);
|
360 |
},
|
361 |
+
drag: (e) => {
|
362 |
+
const { lat, lng } = e.target.getLatLng();
|
363 |
+
const updated = [...geoPoints];
|
364 |
+
updated[index] = { lat, lon: lng };
|
365 |
+
setGeoPoints(updated); // The distance function will be continioust triggered throughout the dragging journey
|
366 |
+
} ,
|
367 |
dragend: (e) => {
|
368 |
const { lat, lng } = e.target.getLatLng();
|
369 |
const updated = [...geoPoints];
|
|
|
380 |
))}
|
381 |
|
382 |
{/* Polyline if 2 points are selected and sidebar is open */}
|
383 |
+
{geoSidebarOpen && geoPoints.length === 2 && (
|
384 |
<Polyline
|
385 |
key={geoPoints.map(pt => `${pt.lat},${pt.lon}`).join('-')}
|
386 |
positions={generateGeodesicPoints(
|