API Reference / API Parameters / aroundLatLngViaIP
Feb. 26, 2019

aroundLatLngViaIP

Type: boolean
Engine default: false
Parameter syntax
'aroundLatLngViaIP' => true|false

Can be used in these methods:

About this parameter

Search for entries around a given location automatically computed from the requester’s IP address.

By computing a central geolocation (from an IP), this has three consequences:

  • a radius / circle is computed automatically, based on the density of the records near the point defined by this setting
  • only records that fall within the bounds of the circle are returned
  • records are ranked according to the distance from the center of the circle

Usage notes:

  • With this setting, you are using the end-user’s IP to define a central axis point of a circle in geo-coordinates.

  • Algolia automatically calculates the size of the circular radius around this central axis.

  • If you are sending the request from your servers, you must set the X-Forwarded-For HTTP header with the front-end-user’s IP address for it to be used as the basis for the computation of the search location.

  • Note: This setting differs from aroundLatLng, which allows you to specify the exact latitude and longitude of the center of the circle.

  • This parameter will be ignored if used along with insideBoundingBox or insidePolygon

  • We currently only support IPv4 addresses. If the end user has an IPv6 address, this parameter won’t work as intended.

Examples

Search around user’s IP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
'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';

$client = new \Algolia\AlgoliaSearch\Client::create(
  'YourApplicationID',
  'YourSearchOnlyAPIKey'
);

$client->setExtraHeader('X-Forwarded-For', $ip);

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

$results = $index->search('query', [
  'aroundLatLngViaIP' => true
]);

Did you find this page helpful?