In this tutorial, we’ll see how we can filter results around a location. This location can either be set manually or taken from the current user position.
// composer autoloadrequire__DIR__.'/vendor/autoload.php';// if you are not using composer// require_once 'path/to/algoliasearch.php';$client=\Algolia\AlgoliaSearch\SearchClient::create('AJ0P3S7DWQ','••••••••••••••••••••ce1181300d403d21311d5bca9ef1e6fb');$index=$client->initIndex('your_index_name');
// for the default versionconstalgoliasearch=require('algoliasearch');// for the default versionimportalgoliasearchfrom'algoliasearch';// for the search only versionimportalgoliasearchfrom'algoliasearch/lite';// or just use algoliasearch if you are using a <script> tag// if you are using AMD module loader, algoliasearch will not be defined in window,// but in the AMD modules of the pageconstclient=algoliasearch('AJ0P3S7DWQ','••••••••••••••••••••ce1181300d403d21311d5bca9ef1e6fb');constindex=client.initIndex('your_index_name');
As we don’t know in advance the Lat/Lng coordinates of the current user, we can rely on the IP address that we’ll automatically associate to a location.
/**
* '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]);
1
2
3
4
5
6
7
8
results=index.search('',{aroundLatLngViaIP: true,headers: {# '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.'X-Forwarded-For':'94.228.178.246'}})
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* '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.
*/index.search('',{aroundLatLngViaIP:true,headers:{'X-Forwarded-For':'94.228.178.246'}}).then(({hits})=>{console.log(hits);});
1
2
3
4
5
6
7
8
9
# '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})
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.client.setHeader(withName:"X-Forwarded-For",to:"94.228.178.246")letquery=Query()query.aroundLatLngViaIP=trueindex.search(query,completionHandler:{(res,error)inprint(res)})
1
2
3
4
5
6
7
8
// '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.
client.setHeader("X-Forwarded-For", "94.228.178.246")
index.search(
new Query().setAroundLatLngViaIP(true)
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// '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.varconfiguration=newSearchConfig("AJ0P3S7DWQ","••••••••••••••••••••ce1181300d403d21311d5bca9ef1e6fb"){DefaultHeaders=newDictionary<string,string>{{"X-Forwarded-For","94.228.178.246"}}};SearchClientclient=newSearchClient(configuration);index.Search<T>(newQuery(""){AroundLatLngViaIP=true});
1
2
3
4
5
6
7
8
9
// '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.index.search(newQuery().setAroundLatLngViaIP(true),newRequestOptions().addExtraHeader("X-Forwarded-For","94.228.178.246"));
1
2
3
4
5
6
7
8
// "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.extraHeaders:=opt.ExtraHeaders(map[string]string{"X-Forwarded-For":"94.228.178.246",})res,err:=index.Search("",opt.AroundLatLngViaIP(true),extraHeaders)
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.client.execute{searchinto"myIndex"queryQuery(query=Some(""),aroundLatLngViaIP=Some(true))optionsRequestsOptions(extraHeaders=Some(Map("X-Forwarded-For"->"94.228.178.246")))}
1
2
3
4
5
6
7
8
9
10
/**
* '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.
*/valquery=query{aroundLatLngViaIP=true}index.search(query,requestOptions{headerForwardedFor("94.228.178.246")})