Spaces:
Runtime error
Runtime error
Merge pull request #8 from DebasishDhal/feat/wiki-geosearch
Browse files- Get coords of all nearby points from backend. Use them to form a bound, add some padding to it, use `flyToBounds` to create a nice animation where map slowly zooms in to your nearby point cluster area
- Replace slider with keyboard for explorationRadius
- Fix incorrect "Found ${page_count} nearby pages" on frontend
frontend/src/components/Map.js
CHANGED
@@ -87,14 +87,29 @@ const Map = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSubmitt
|
|
87 |
const [explorationLimit, setExplorationLimit] = useState(10);
|
88 |
const [explorationMarkers, setExplorationMarkers] = useState([]);
|
89 |
const [explorationSidebarOpen, setExplorationSidebarOpen] = useState(false);
|
|
|
90 |
|
91 |
-
|
|
|
92 |
const map = useMap();
|
93 |
useEffect(() => {
|
94 |
if (position && Array.isArray(position) && position.length === 2) {
|
95 |
map.setView(position, map.getZoom());
|
96 |
}
|
97 |
}, [map, position]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
return null;
|
99 |
};
|
100 |
|
@@ -246,7 +261,8 @@ const Map = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSubmitt
|
|
246 |
},
|
247 |
...markers
|
248 |
]);
|
249 |
-
|
|
|
250 |
} else {
|
251 |
console.error('Failed to fetch nearby pages');
|
252 |
}
|
@@ -592,7 +608,11 @@ const Map = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSubmitt
|
|
592 |
<ScaleControl position="bottomright" imperial={true} />
|
593 |
|
594 |
<ResizeHandler trigger={wikiWidth} />
|
595 |
-
<CenterMap
|
|
|
|
|
|
|
|
|
596 |
{baseLayer === "satellite" && (
|
597 |
<>
|
598 |
<TileLayer
|
@@ -965,13 +985,37 @@ const Map = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSubmitt
|
|
965 |
type="range"
|
966 |
min="10"
|
967 |
max="10000"
|
968 |
-
step="1000"
|
969 |
value={explorationRadius}
|
970 |
onChange={(e) => setExplorationRadius(parseInt(e.target.value))}
|
971 |
style={{ width: '100%' }}
|
972 |
/>
|
973 |
-
<div style={{
|
974 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
975 |
</div>
|
976 |
</div>
|
977 |
|
@@ -1025,7 +1069,7 @@ const Map = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSubmitt
|
|
1025 |
fontSize: '14px',
|
1026 |
color: '#1976d2'
|
1027 |
}}>
|
1028 |
-
Found {explorationMarkers.length} nearby pages
|
1029 |
</div>
|
1030 |
)}
|
1031 |
|
|
|
87 |
const [explorationLimit, setExplorationLimit] = useState(10);
|
88 |
const [explorationMarkers, setExplorationMarkers] = useState([]);
|
89 |
const [explorationSidebarOpen, setExplorationSidebarOpen] = useState(false);
|
90 |
+
const [shouldZoom, setShouldZoom] = useState(false);
|
91 |
|
92 |
+
// Using CenterMap component to handle centering (for summary/full apis) and for zooming (for wiki/nearby api)
|
93 |
+
const CenterMap = ({ position, coordinates, shouldZoom }) => {
|
94 |
const map = useMap();
|
95 |
useEffect(() => {
|
96 |
if (position && Array.isArray(position) && position.length === 2) {
|
97 |
map.setView(position, map.getZoom());
|
98 |
}
|
99 |
}, [map, position]);
|
100 |
+
|
101 |
+
|
102 |
+
useEffect(() => {
|
103 |
+
if (coordinates && Array.isArray(coordinates) && coordinates.length > 0 && shouldZoom) {
|
104 |
+
const bounds = L.latLngBounds(coordinates);
|
105 |
+
map.flyToBounds(bounds, {
|
106 |
+
padding: [50, 50],
|
107 |
+
maxZoom: 16,
|
108 |
+
duration: 3
|
109 |
+
});
|
110 |
+
}
|
111 |
+
}, [coordinates, map, shouldZoom]);
|
112 |
+
|
113 |
return null;
|
114 |
};
|
115 |
|
|
|
261 |
},
|
262 |
...markers
|
263 |
]);
|
264 |
+
setShouldZoom(true);
|
265 |
+
console.log(`Found ${markers.length} nearby pages`); // Only backend results.
|
266 |
} else {
|
267 |
console.error('Failed to fetch nearby pages');
|
268 |
}
|
|
|
608 |
<ScaleControl position="bottomright" imperial={true} />
|
609 |
|
610 |
<ResizeHandler trigger={wikiWidth} />
|
611 |
+
<CenterMap
|
612 |
+
position={markerPosition}
|
613 |
+
coordinates={explorationMarkers.map((marker) => marker.position)}
|
614 |
+
shouldZoom={shouldZoom}
|
615 |
+
/>
|
616 |
{baseLayer === "satellite" && (
|
617 |
<>
|
618 |
<TileLayer
|
|
|
985 |
type="range"
|
986 |
min="10"
|
987 |
max="10000"
|
|
|
988 |
value={explorationRadius}
|
989 |
onChange={(e) => setExplorationRadius(parseInt(e.target.value))}
|
990 |
style={{ width: '100%' }}
|
991 |
/>
|
992 |
+
<div style={{
|
993 |
+
display: 'flex',
|
994 |
+
alignItems: 'center',
|
995 |
+
gap: '8px',
|
996 |
+
marginTop: 4,
|
997 |
+
justifyContent: 'center'
|
998 |
+
}}>
|
999 |
+
<input
|
1000 |
+
type="number"
|
1001 |
+
min="10"
|
1002 |
+
max="10000"
|
1003 |
+
value={explorationRadius}
|
1004 |
+
onChange={(e) => {
|
1005 |
+
const value = parseInt(e.target.value);
|
1006 |
+
if (value >= 10 && value <= 10000) {
|
1007 |
+
setExplorationRadius(value);
|
1008 |
+
}
|
1009 |
+
}}
|
1010 |
+
style={{
|
1011 |
+
width: '80px',
|
1012 |
+
padding: '4px 8px',
|
1013 |
+
border: '1px solid #ccc',
|
1014 |
+
borderRadius: '4px',
|
1015 |
+
textAlign: 'center'
|
1016 |
+
}}
|
1017 |
+
/>
|
1018 |
+
<span>m</span>
|
1019 |
</div>
|
1020 |
</div>
|
1021 |
|
|
|
1069 |
fontSize: '14px',
|
1070 |
color: '#1976d2'
|
1071 |
}}>
|
1072 |
+
Found <span style={{ fontWeight: 'bold' }}>{explorationMarkers.length-1}</span> nearby pages
|
1073 |
</div>
|
1074 |
)}
|
1075 |
|