Reputation: 31
I'm currently building a website/web app that will pull "attractions" data from Algolia and then display it - essentially a very simple search/filter web app.
When the user finds the attraction they want, they click on it and more detail is shown (description, Google Map location, address etc). One thing that I want it to show is other attractions near by (5 mile radius).
Now, having been on the Algolia docs a fair bit I've found that this is possible using AlgoliaSearchHelper (see: https://www.algolia.com/doc/guides/geo-search/geo-search-overview#filter-and-sort-around-a-location)
Long story short, I've managed to get Algolia to give me attractions in a 5 mile radius from a point, my problem lies with the fact that that Longitude and Latitudes are manually entered rather than dynamic (would be pulled from _geoloc in the Algolia object).
What I currently have is:
var algoliaHelper = algoliasearchHelper(client, INDEX_NAME);
algoliaHelper.setQueryParameter('getRankingInfo', true);
algoliaHelper.setQueryParameter('aroundLatLng', '51.7664027,-0.4747815');
algoliaHelper.setQueryParameter('aroundRadius', 118047);
algoliaHelper.search();
(Note: aroundRadius is set to 118047 as I don't actually have any other attractions within 5 miles, this is for testing only at the stage)
This works great, it gives me the closest attractions in distance order and displays the distance as I've used Algolia's docs again to produce this:
if (hit._rankingInfo.matchedGeoLocation) {
hit.distance = '(' + parseInt(hit._rankingInfo.matchedGeoLocation.distance * 0.00062137, 10) + 'm)';
}
If I try and change this to dynamic entries (being pulled from the attraction's _geoloc data and convert to a string for example:
var test = String(content._geoloc.lat + "," + content._geoloc.lng);
var algoliaHelper = algoliasearchHelper(client, INDEX_NAME);
algoliaHelper.setQueryParameter('getRankingInfo', true);
algoliaHelper.setQueryParameter('aroundLatLng', test);
algoliaHelper.setQueryParameter('aroundRadius', 118047);
algoliaHelper.search();
The results still come in... but the distance is 0 and when I console.log() the object I see under _rankingInfo object that geoDistance is 9 which suggest it's not working as originally the distance for the same data is 42073 meters.
My question is, is there a way of using the _geoloc details from my selected attraction to pull other attractions within a radius dynamically (as when I go onto another attraction it could be in a totally different location with different attractions nearby).
I appreciate any help I can get with this, I wouldn't be shocked that there's a huge gap in my JS knowledge and this is very obvious.
Thanks in advance.
Upvotes: 2
Views: 989
Reputation: 31
Well, after a lot of time deleting and updating code, the above code appears to work perfectly. The main issue was my object coming from Algolia and I didn't notice.
I'm afraid frustration mixed with staring at the same code for too long blinded me on this one.
Upvotes: 1