Filter results around a location
Use Algolia’s geo search capabilities to filter results around a location.
This guide shows you how to filter results around a location. This location can be set manually or taken from the a user’s current position.
Dataset
The guide uses a dataset of the 3,000+ biggest airports in the world.
Store each record’s location (latitude and longitude) in the _geoloc
attribute.
First, download the airport dataset and then import it into Algolia.
Initialize the client
Configure index settings
Even if you just want to sort by distance to a location, your textual relevance should also be good so that users can refine the search with a query. To do that, you must configure the index.
The searchable attributes are: name
, city
, country
, and iata_code
.
Custom ranking
The engine will use an airport’s number of connected airports as a ranking metric. The more connections, the better.
Ranking
When filtering around a location, Algolia can also sort the results by distance from this location. This sorting by distance happens in the ranking formula’s geo
criterion. If geo
isn’t active, you can’t sort by distance.
Filtering around a given location
To filter airports around New York City (latitude 40.71 and longitude -74.01), use the aroundLatLng
parameter.
An empty query (''
) tells Algolia to retrieve all airports
Filtering around the user’s current location
As you don’t know your user’s coordinates in advance, you can retrieve their IP address’s associated location with the aroundLatLngViaIP
parameter.
Filtering by radius
By default, the engine automatically defines a radius to filter on depending on the population density of the user’s location.
To define the radius yourself, use the aroundRadius
parameter. The bigger the radius, the less filtering you have.
This example sorts by distance to New York City, with a radius of 1,000 km.
Was this page helpful?