This is the React InstantSearch v7 documentation.
If you’re upgrading from v6, see the upgrade guide.
If you were using React InstantSearch Hooks,
this v7 documentation applies—just check for necessary changes.
To continue using v6, you can find the archived documentation.
useGeoSearch()
Hook.
This Hook isn’t tied to any map provider, so you can select and implement any solution.
This guide uses Leaflet as the map provider through its React wrapper, React Leaflet.
To learn more about loading and using leaflet, visit the Leaflet documentation.
Dataset
To follow along, you can use a dataset with more than 3,000 airports. You can download the dataset on GitHub. See Importing from the dashboard to learn how to upload the dataset to Algolia.JSON
_geoloc
attribute.
Configure index settings
When displaying on a map, you still want the relevance of your search results to be good. To achieve that, you need to configure:- Searchable attributes—support searching by:
name
,city
,country
andiata_code
. - Custom ranking—use the number of other connected airports
links_count
as a ranking metric. The more connections the better.
Display hits on the map
Add the Leaflet map container to your React InstantSearch app. By default, the map container has height 0, so make sure to set an explicit height, for example, using CSS.React
useGeoSearch()
Hook you created before.
React
MapContainer
.
You should now see markers representing the airports locations.
Move the map
To add some interactivity, you can detect users interactions and update the list of airports to match the new viewable area of the map.React
React to search query updates
In the current form, if you type a query in the search box, for example, “Italy”, the markers disappear. That’s because the list of markers now show all the airports in Italy, but the map is still in its initial location. To move the map when the search query changes, use theuseSearchBox
Hook to retrieve the current query,
and move the map programmaticaly when it changes.
To not interfere with the manual movement of the map, you need to clear the boundaries refinement when a query changes.
In the onViewChange
event handler, you also need to reset the query and set a flag that instructs the application to not move the map programmatically in this case.
React