🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
UI libraries / React InstantSearch / Widgets

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 useGeoSearch hook lets you display search results on a map. Use it to search for results based on their location and features like “search on map interactions”.

Examples

Geo search with React Leaflet

OpenStreetMap contributors' url=https://www.algolia.com/doc/api-reference/widgets/geo-search/react/"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> ); }" class="snippet-body ">
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='&copy; <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>
  );
}
OpenStreetMap contributors' url=https://www.algolia.com/doc/api-reference/widgets/geo-search/react/"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> ); }" class="snippet-body hidden">
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
45
46
47
48
49
50
51
52
53
import { useGeoSearch, UseGeoSearchProps } from 'react-instantsearch';
import { LeafletEvent } from 'leaflet';
import {
  MapContainer,
  Marker,
  Popup,
  TileLayer,
  useMapEvents,
} from 'react-leaflet';

type Airport = {
  airport_id: string;
  city: string;
  country: string;
  name: string;
  nb_airline_liaisons: number;
};

export function CustomGeoSearch(props: UseGeoSearchProps) {
  const { items, refine } = useGeoSearch<Airport>(props);

  function onViewChange({ target }: LeafletEvent) {
    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='&copy; <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>
  );
}
Did you find this page helpful?