Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 634143b

Browse files
vvoredox
authored andcommitted
feat(precision): add useDeviceLocation option (#210)
This will ask and use the browser geolocation API.
1 parent 5ac5f5a commit 634143b

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

docs/source/documentation.html.md.erb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,26 @@ If using the authenticated API, the API key to use.
238238
</tr>
239239
<tr>
240240
<td markdown="1">
241+
<div class="api-entry" id="api-options-useDeviceLocation"><code>useDeviceLocation</code></div>
242+
243+
Type: **boolean**
244+
</td>
245+
<td markdown="1">
246+
Ask and use the device location.
247+
</td>
248+
</tr>
249+
<tr>
250+
<td markdown="1">
251+
<div class="api-entry" id="api-options-computeQueryParams"><code>computeQueryParams</code></div>
252+
253+
Type: **object**
254+
</td>
255+
<td markdown="1">
256+
Allows you to override computed query parameters sent to the [Algolia Places REST API](rest.html).
257+
</td>
258+
</tr>
259+
<tr>
260+
<td markdown="1">
241261
<div class="api-entry" id="api-options-clientOptions"><code>clientOptions</code></div>
242262

243263
Type: **object**
@@ -519,7 +539,7 @@ If you're already using instantsearch.js, you can use Algolia Places out of the
519539
widget will do a search of places using Algolia Places and when an adress is selected, it
520540
will update the geolocation point of the instantsearch.js search.
521541

522-
You can include it using a link tag:
542+
You can include it using a link tag:
523543

524544
```html
525545
<%= javascript_include_tag config[:places_instantsearch_widget_cdn_url] %>

docs/source/javascripts/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
const $input = document.querySelector('#landing-demo');
55
const placesAutocomplete = places({
6-
container: $input
6+
container: $input,
7+
useDeviceLocation: true
78
});
89
$input.style.opacity = 1; // we initially hide the input to avoid size flickering
910

src/createAutocompleteSource.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export default function createAutocompleteSource({
1111
aroundLatLngViaIP,
1212
countries,
1313
formatInputValue,
14+
computeQueryParams = params => params,
15+
useDeviceLocation = false,
1416
language = navigator.language.split('-')[0],
1517
onHits = () => {},
1618
type
@@ -39,11 +41,19 @@ export default function createAutocompleteSource({
3941
defaultQueryParams.aroundRadius = aroundRadius;
4042
}
4143

44+
let userCoords;
45+
if (useDeviceLocation) {
46+
navigator.geolocation.watchPosition(
47+
({coords}) => userCoords = `${coords.latitude},${coords.longitude}`
48+
);
49+
}
50+
4251
return (query, cb) => placesClient
43-
.search({
52+
.search(computeQueryParams({
4453
...defaultQueryParams,
54+
[userCoords ? 'aroundLatLng' : undefined]: userCoords,
4555
query
46-
})
56+
}))
4757
.then(
4858
content => {
4959
const hits = content.hits.map((hit, hitIndex) => {

0 commit comments

Comments
 (0)