Hello, for application purposes I am looking to create a system where users can search for places by distance.

I do not use a plugin referring to google api or other

I have a User table with common (text) latitude (number) and longitude (number) fields.

I have another Place table where I have different fields including latitude (number) and (longitude) number.

With the Ultimate Toolkit plugin I would like to create a workflow when I click on a button calculates the distance between a user and the different Places, knowing that I have 9000 Places. Maybe I should save the result of this query in a user field?

Can you tell me if my screenshot below is good and what is missing in the to List function as I can’t seem to get past it.

Thanks for your help

A few days ago I came across this link

I think that corresponds to my research but I stumble on bubble logic

Here is the excerpt that interests me

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. "

So I created a new type called relationship with this inside.

Does my distanceresult field have to be a number

And now I don’t see how I can fill this in my workflow

help please :slight_smile:
thanks

1 Like

If you can set a max radius for the search you won’t need then iterate 9000 places - just those that are within the max radius. Your data structure looks correct. You would need to create a backend workflow to calculate the distances.

Hi @shpak.serhiy

all my searches on “radius” send me tutorials where they talk about it but with the google API. I can’t find how to calculate the radius with ‘Ultimate toolkit’, you wouldn’t have a screenshot or a walkthrough or video for that?

Thx

It is a bit complicated to describe it in short and clear. Let’s say you need to calculate the distance from A to B(n).
The trick is to filter and sort all B(n) by longitude and latitude in relation to A
You need to run a recursive workflow on the list of things to which you want to calculate the distance.
You will have 4 lists of Things:

  1. Lon B > Lon A | Lat B > Lat A
  2. Lon B < Lon A | Lat B < Lat A
  3. Lon B < Lon A | Lat B > Lat A
  4. Lon B > Lon A | Lat B < Lat A

As the Bs are sorted in the lists the workflow will process them not randomly but starting from the center (A). Once the workflow exceeds the radius it stops.

So you might have 9000 things in your DB. But for instance, in a 50 km radius, there might be 9 things and those things could be found almost instantly and without engaging paid APIs.

2 Likes

Thank you for your help and your patience. I’ll see how I can get out of this!

Hi @cardunercedric Did you manage to calculate distances ? I have exactly the same issue:


If you can help I would be more than happy :wink:

Hello,

It depends on what kind of distance you are looking for, and how accurate you need to be. So for instance, if you are just looking for a local driver’s GPS route, it’s not an issue to simply use the built-in function to cheek the distance between two locations. But there might be times when you need more functionality. Also, I haven’t checked yet, but if you are doing long distances, you might want to double check that bubble actually does distances properly. There is a specific function to calculate distance over large areas due to the earth being round, and the quickest way to a far off location isn’t actually a straight line. Beyond that, if you need epically accurate long range distance, then you get into GIS type stuff, and even the world itself isn’t 100% the same round shape everywhere.

In my app I actually went the Supabase route because it’s Postegres SQL and I need additional functions, but it also has a built in accurate distance between two point function. So depending on how many distances you need to figure out, possibly that might be a better option.

So maybe give more details in here, and I’ll help you as I can.

1 Like

thanks a lot for your quick reply @troy.roberge :wink:

To clarify, my app take as input a location like a city or address input by a user (I convert the input address into lat and long using google API and I put that data into the database USER as a temporary position since the input can be modify at any time by the user). Then I want to display the distance of a list of several hundred of events around the input city (only straight line distance, no need to be accurate), which are in the database (lat and long as well). Right now I use the google API and it works perfectly fine but the cost is becoming to high… for instance if 100 users check events around a given position and If I have 100 events in the DB, it require 10K API call to google… :scream:

So I need to find a way to calculate that distance using a back-end workflow without using the google API (except for catching the lat and long of the user input). I saw only the Ultimate tool kit as a solution, but I don’t understand how it works (no doc available)… Also I though using Math plugin to make the calculation of the distance equation but acos, cos, sin algebra doesn’t seems to work… I kind of stuck right now… thanks a lot for your help :wink:

So I am not certain how fast your app will be (you will need to test), but you don’t need an API to do this. You can do distances inside of Bubble.

Try this. It’s front end, but I am sure you can figure out the back end version.

image
image
image
image

Interesting thx a lot, are you sure that is not using Google API some how ?

Well that’s a good point, try it out without a Google API key set up and see if it works!

So yes, they do use the Google API, because you would have put that into your app. But you do get a $200 credit every month from google, so that should cover a large amount of costs. Aside from that, you can always check out other map providers like mapbox.

Thank you @troy.roberge. So I finally manage to make calculation of the distances using Mathcalculator and a simplify equation assuming the earth is flat :wink: it works for small distance…


Then I went to my google cloud account and observe no difference in the API call… So I investigated for quite some time and observe that Google place API with the auto completion feature use to perform 5 API call for only 1 input… Also I observed that all calculation distances inside bubble are not passing by google once you have in the DB the location… which is a good news ! So I simply disabled the place API since auto completion will be too expensive for me if I have many users… and also the input geo adress works already well in bubble (allow typo…). Now I use only the Google Geocoding API and it works very well: 1 api call for 1 input, and all the distances are calculated inside bubble :grinning: Thank u again for your help !

Awesome. Just an FYI, you can simply do distances between addresses if you don’t want to work with the long and lat. But if you are using long and lat, add more zeros to the decimal place. It really needs all of them. You can slow the final result without so many though.

Hi, how could I set a max radius? I’m trying something similar.