useGeoSearch()
This is the React InstantSearch v7 documentation. React InstantSearch v7 is the latest version of React InstantSearch and the stable version of React InstantSearch Hooks.
If you were using React InstantSearch v6, you can upgrade to v7.
If you were using React InstantSearch Hooks, you can still use the React InstantSearch v7 documentation, but you should check the upgrade guide for necessary changes.
If you want to keep using React InstantSearch v6, you can find the archived documentation.
About this Hook
The geoSearch
hook lets you search for results based on their position within a specified area (a bounding box).
It also provides features such as “search on map interactions”.
The geoSearch
hook doesn’t let you search around a central point or
within polygons.
If that’s important to you, you must use the Algolia API instead.
Examples
Geographical search with React Leaflet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { useGeoSearch } from 'react-instantsearch';
import {
MapContainer,
Marker,
Popup,
TileLayer,
useMapEvents,
} from 'react-leaflet';
export function CustomGeoSearch(props) {
const { items, refine } = useGeoSearch(props);
function onViewChange({ target }) {
refine({
northEast: target.getBounds().getNorthEast(),
southWest: target.getBounds().getSouthWest(),
});
}
useMapEvents({ zoomend: onViewChange, dragend: onViewChange });
return (
<MapContainer
center={[48.85, 2.35]}
zoom={10}
minZoom={4}
scrollWheelZoom={true}
>
<TileLayer
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
{items.map((item) => (
<Marker key={item.objectID} position={item._geoloc}>
<Popup>
<strong>{item.name}</strong>
<br />
{item.city}, {item.country}
</Popup>
</Marker>
))}
</MapContainer>
);
}