Do all distance functions in Bubble use Google APIs?

I’m a bit confused. I have a Bubble app on its own domain and with the Google API keys set up.

I have data items with geographic addresses and on a page I return lists of those items searched for and sorted based on “distance”. There are no maps.

I assumed all of the distance functions used Google APIs, but I don’t seem to see them coming through in my Google dashboard’s metrics.

I’d actually be really happy if these functions didn’t use Google APIs as I fear it would become prohibitively expensive - I do a lot of distance stuff but it’s all quite basic. Simple “as the crow flies” (Haversine) distance calculations would be perfectly adequate - I don’t need them to be route based.

Is there any chance the distance search/sort stuff in Bubble uses its own calculations based on latitude and longitue, rather than using Google APIs?

Thanks, James.

1 Like

Hoping someone can help with this. Ideally I’d just like to know what happens behind the scenes when I search for data ‘within 20 miles’ of current page’s location, and when the resulting list is sorted by distance.

Which APIs does it call (if any)? If I have 10 lists on a page with 3 items in each, is that 30 calls to a Google API per page load?

Just to reiterate, I’m not using maps at all, just distances based on geographic addresses.

Thanks.

1 Like

If you need to calculate distance between coordinates, then there are two plugins for it. And you can calculate this dance without any API.

Thanks @shpak.serhiy. Which two plugins are those please?

And can you give me a couple of hints on how to populate a repeating group with e.g. all [things] that are within 20 miles of [this thing]?

James

ToolitKit https://bubble.io/plugin/toolit-kit---helpful-functions-1562812673262x109396480465567740
has distance plugin that calculate distance between two coordinates.

To create a list of all things within 20 miles you need to iterate your list of thing and calculate the distance between every thing from the list and “this thing”. If distance is <20 miles you can store it in a list or mark somehow.

If you have too many things and don’t want to iterate all of them (using API workflow or ListShifter) you could create four separate list of things based on Lon/Lat.

  1. lat > this thing lat
  2. lon > this thing lon
  3. lat < this thing lat
  4. lon > this thing lon

Than you stop iterate the list once you exceed the 20 miles distance and hence you won’t iterate the whole list of things.

Basically, it is a rough algorithm that you can build in Bubble.

1 Like

Hi @shpak.serhiy ,
How can I use the toolit kit plugin to sort a repeating group by distance? The database has the Latitude and Longitude fields. I have struggled to find documentation for the distance function of the plugin. Kindly help.

If the location of the things is stable you could store the distances in the database.

How to do it:

  • Create a new datatype “Relation” that will include reference to two things and a distance field.
  • Create a backend workflow that is triggered when a new thing is added. The workflow should iterate all things and create “Relations”.
  • then you can use “Relation” for sorting.

If it is not your use case and you need to calculate the distance every time within a certain radius then a different a bit more complex approach could be used. It requires more explanation. Let me know you need it.

Thanks for your quick response.
The location of the things (Pharmacy data type) remains the same. However, every time the RG loads, the distance between the Pharmacy and the current user’s location (which changes) has to be calculated and displayed.
My goal is to display that distance in each cell of the RG and sort it by that same distance (closest to furthest).
e.g. Pharmacy A - - - 1.5KM
Pharmacy B - - - 2.3KM
To get the user’s location I used the Toolbox plugin JavascriptToBubble element (with an HTML5 geolocation script) and used the Expression element to compute and display the straight-line distance in the RG cells.
I’d like to avoid creating a distance field that would have to be constantly updated for so many rows.

I guess my question is: Is there a way I could sort the Pharmacy by distance without using the Google Distance Matrix API? Knowing that the distance is already computed and displayed in a Text element in the RG cells.
The Pharmacy data type structure is: Name, Latitude, Longitude

Sorting is possible on the fields of the datatype that set on the RG. The calculated data is not a field and hence is not available for sorting in the RG data source settings.

You can solve this issue by creating a field on the Thing you sort where you will temporary store the distance. You can populate the distance to this field automatically triggering by “When page is loaded” → make change to the list of things (here you indicate the expression that calculates the distance).

Thank you. I’ll apply that.