Instantsearch & Places - Get Location Data

On my search page, among the refinement options, I have a location field connected to Algolia Places. From what I understand, this sets the aroundLatLng of the search. I have aroundLatLngViaIP set to true.

What I would like is to be able to extract the ‘currently set’ location from this widget and display that information. For example, if a user from Houston, TX was to use my search, then their first search would search around Houston. I would like to be able to display “Searching From: Houston, Texas” somewhere on the page. And that should update when the Places input is used to choose a new location to “search from.” I would also like this to be reflected via the URL, with routing.

I’ve found that using search.helper.lastResults.aroundLatLng sometimes (not always?) contains the latitude and longitude, so perhaps I can use that to get the information I am looking for. But I don’t know how.

I’m using instantsearch.js.

Hi,

If I understand your issue correctly, you would like to be able to display to your users that their search is being restricted to a certain location before they start interacting with your search, and even if they don’t interact with the places widget. Is that correct?

If so, then there is no simple way of doing that just using the Places widget, but it is still feasible: You can do that by doing a first query to places when setting up InstantSearch and Places and update the placeholder value of your Places Widget input.

To do that you will need to add algoliasearch to your list of dependencies and you can then call algoliasearch.initPlaces('yourPlacesAppId', 'yourPlacesApiKey') to get a places instance, on which can then call .search(...), and when this resolves you can then update the placeholder value of your Places widget instance.

I have set up a simple codesandbox with a demo of how to do that: https://codesandbox.io/s/wq7ypyk995

This is a bit hacky, but it should solve your issue. I strongly suggests sticking to updating the placeholder value if you want to enable routing, i.e. if you want synchronise your InstantSearch state with the URL.