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
This guide use a dataset of over 3,000 of the biggest airports in the world.JSON
_geoloc attribute to enable geo-filtering and geo-sorting.
You can download the dataset on GitHub.
Have a look at how to import it in Algolia.
Configure index settings
When displaying on a map, you still want the relevance to be good. For that, configure the as follows:- Searchable attributes should be set to enable search in the four textual attributes:
name,city,country, andiata_code. - Custom ranking: use the number of other connected airports
links_countas a ranking metric. The more connections the better.
Display hits on the map
Add the Leaflet map container
By default, the map container has height 0, so make sure to set an explicit height, for example, using CSS.
React
Create custom widget
To populate the map with markers, create a custom widget for React InstantSearch, using the
useGeoSearch() Hook you created before.React
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