Format location data to include a distance radius

Hi,

The records in my search have a lat & long value so that their location can be searched.

Some of these records are also available within a certain distance e.g. within 5 miles, within 20 miles, etc.

How can I format my records location data (lat & long or otherwise) to incorporate a travel radius?

For example, a record could have the lat & long of Birmingham saved to Algolia and the record could be available within 20 miles of Birmingham.

So what I want is for when a user searches a location within 20 miles of Birmingham for that record to be included in the search.

How can I achieve this by saving my records data to Algolia?

Thanks in advance for any help.

Hi @alex8!

For this precise example, if you would like to have a specific record to appear in a search from a user located within 20 miles of Birmingham, you would have to:

As you can see, the radius is set at query time, it is not part of the record data. So for a given query, you can decide to retrieve all the results within a certain distance, but you can’t change this range depending on the records for the same query.

I hope this is clear and this is what you were looking for. Let us know if you have more questions!

Hi, @marie.gillier,

Once again thanks for your help, this is well explained and very helpful.

Just to point out my records have a location e.g. Birmingham but these records can also add a “travel distance” this could be in “miles” or the whole UK.

So what I’m hoping to achieve is when a record is located in Birmingham but states that they will “travel up to 20 miles” that when a user searches Wolverhampton (which is 17 miles away) that record will appear in the search because that record states it would travel up to 20 miles.

Does that make sense and is that possible?

Thanks for your continued help.

Hmm, this makes a lot of sense and it is an interesting use case, but I’m afraid that this is not possible for now.
Since the radius is set in the query, it cannot be changed dynamically based on a value located in the records returned by this query or to recompute dynamically the location of a record.

The best workaround I can think of, even if not as satisfying, is to check the amount of results returned by a user query, and if it is small, then prompt the user to search for a wider radius where hopefully the record in Wolverhampton would pop in.

Have a nice evening!

Hi, @marie.gillier,

Thanks again for your reply and I hope you’re well during this slightly noticeable global pandemic.

This is a shame I think this might be a case of our requirements for our search being a bit too complicated but still, it would be nice if it could work this way.

So there is no way of providing a record’s location as a radius itself?

e.g. instead of a lat & long that focuses on a single specific location, a location could be stored that is a radius around a specific location like a circle (perhaps that’s a bit of a ridiculous idea) but still is that maybe possible?

Thank you so much for your help & patience and with putting up with my complicated & ridiculous questions, you’re a star :slightly_smiling_face:

Hi Alex,

Unfortunately, this is not possible to do directly as a query within Algolia. However, you might be able to create a workaround by looping over the results you receive back from Algolia. Please note that this workaround has its limitations, and will probably impact the performance of your search.

When you do a geolocation search with a filter for location (this means using one of the following parameters: aroundLatLng, aroundLatLngViaIP, insideBoundingBox or insidePolygon), you will receive a _rankingInfo.matchedGeoLocation object. This object contains the distance in meters from the location you’re trying to filter.

If you add information to each record about their travel distance (in meters), you could loop over all the hits you receive from Algolia before displaying them, and filter out all the results for which the _rankingInfo.matchedGeoLocation.distance is bigger than the travel distance of the record.

As I said earlier, this approach does have some downsides. Let’s say you only want to display 10 results. To be sure you have at least 10 results to display, you might have to fetch more results from Algolia than you actually need. If you would only fetch the 10 results, and 3 of them have a travel distance smaller than the actual distance, you would only have 7 results to display.

On top of that, looping over all the records and not displaying certain objects has an impact on the performance of the page as well, as it will take slightly longer to display the results than normally.

I hope I understood the problem correctly and this workaround helps, good luck with the implementation!

1 Like