> ## Documentation Index
> Fetch the complete documentation index at: https://www.algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# useGeoSearch

> React Hook for location-based search inside a bounding box on a map.

> [!NOTE]
>
> This is the **React InstantSearch v7** documentation.
> If you're upgrading from v6, see the [upgrade guide](https://www.algolia.com/doc/guides/building-search-ui/upgrade-guides/react.md#migrate-from-react-instantsearch-v6-to-react-instantsearch-v7).
> If you were using React InstantSearch Hooks,
> this v7 documentation applies—just check for [necessary changes](https://www.algolia.com/doc/guides/building-search-ui/upgrade-guides/react.md#migrate-from-react-instantsearch-hooks-to-react-instantsearch-v7).
> To continue using v6, you can find the [archived documentation](https://algolia.com/old-docs/deprecated/instantsearch/react/v6/api-reference/instantsearch/).

## 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".

> [!NOTE]
>
> The `geoSearch` Hook doesn't let you [search around a central point](https://www.algolia.com/doc/guides/managing-results/refine-results/geolocation/how-to/filter-results-around-a-location.md)
> or [within polygons](https://www.algolia.com/doc/guides/managing-results/refine-results/geolocation/how-to/filter-results-inside-a-polygonal-area.md).
> If you want this, you need to build your own UI on top of the Algolia API.

> [card] [Open CodeSandbox](https://codesandbox.io/s/github/algolia/doc-code-samples/tree/master/react-instantsearch/geo-search?file=/src/Airports.tsx)
>
> Run and edit the useGeoSearch example in CodeSandbox.

## Example: React Leaflet

```jsx JavaScript theme={"system"}
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>
  );
}
```

```tsx TypeScript theme={"system"}
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>
  );
}
```
