Guides / Managing results / Refine results / Geo location

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 end user’s current position.

Dataset

The guide uses a dataset of the 3,000+ biggest airports in the world.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
 {
    "objectID": "3797",
    "name": "John F Kennedy Intl",
    "city": "New York",
    "country": "United States",
    "iata_code": "JFK",
    "_geoloc": {
      "lat": 40.639751,
      "lng": -73.778925
    },
    "links_count": 911
  }
]

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

1
2
3
4
5
6
7
8
9
10
11
12
// composer autoload
require __DIR__ . '/vendor/autoload.php';

// if you are not using composer
// require_once 'path/to/algoliasearch.php';

$client = \Algolia\AlgoliaSearch\SearchClient::create(
  'YourApplicationID',
  'YourAdminAPIKey'
);

$index = $client->initIndex('your_index_name');

Configure index settings

Even if you just want to sort by distance to a location, your textual relevance should also be good so that end 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.

1
2
3
4
5
6
7
8
9
10
11
$index->setSettings([
  'searchableAttributes' => [
    'name',
    'city',
    'country',
    'iata_code'
  ],
  'customRanking' => [
    'desc(links_count)'
  ]
]);

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.

1
2
3
$results = $index->search('', [
  'aroundLatLng' => '40.71, -74.01'
]);

An empty query ('') tells Algolia to retrieve all airports

Filtering around the end user’s current location

As you don’t know the end user’s coordinates in advance, you can retrieve their IP address’s associated location with the aroundLatLngViaIP parameter.

1
2
3
4
5
6
7
8
9
10
11
/**
 * '94.228.178.246' should be replaced with the actual IP you would like to search around.
 * Depending on your stack there are multiple ways to get this information.
 */

$ip = '94.228.178.246';

$results = $index->search('', [
  'aroundLatLngViaIP' => true,
  'X-Forwarded-For' => $ip
]);

Filtering by radius

By default, the engine automatically defines a radius to filter on depending on the population density of the end 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.

1
2
3
4
$results = $index->search('', [
  'aroundLatLng' => '40.71, -74.01'
  'aroundRadius' => 1000000 // 10000 km
]);
Did you find this page helpful?