I’m building a jobs board type app for a niche area I work in. Part of my functionality is returning a list of jobs to the user in a RG presented as a table. One column is ‘Distance’ - the distance from the current user to the job location. To minimize calls to the Google APIs, I store the home location of the user in the User table and the location of the job site in the Jobs table. So in my RG I’m just using the built-in Bubble ‘distance between’ function. However this uses Google API calls and I’m worried if I have 100s of users with 100s of jobs listed, I’m looking at tens of thousands of API requests and $$$! I came across the Ultimate Toolkit Plugin with the Distance function. I’ve installed it and played around a bit - have it firing off a button click to calculate a distance but I don’t understand how I would incorporate it into calculating a bunch of distances in a RG?
Or any other ideas how I could do this without making lots of Google API calls per month?
Hi! If you’re storing the coordinates of these places in your DB, you can use the following code to get the distance between the places
function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
function deg2rad(deg) {
return deg * (Math.PI / 180);
}
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2 - lat1);
var dLon = deg2rad(lon2 - lon1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var distance = R * c; // Distance in km
return distance;
}
// Example usage:
var distance = getDistanceFromLatLonInKm(51.5074, -0.1278, 40.7128, -74.0060);
console.log("Distance: " + distance + " km");
Once you get this, you can then sort by for each user. Let me know if this works
Hi Prashant, many thanks for that! I came across that calculation somewhere before and wasn’t sure how to use it in Bubble but I will definitely investigate it further now. I’ve never used JS script in Bubble before but have a traditional Java programming background so should be able to work it out. Many thanks again Prashant - it’s not the first time you’ve helped me!!
I haven’t actually verified the fact yet - but does the Bubble distance function definitely consume Google API calls - if you already have the location data stored in the DB?
You can use the Toolbox plugin (I use this in all of our client applications) to inject custom JS using the Run Javascript workflow action in sync with the Javascript to Bubble element
Regarding the distance function, I might need to check on this cause I’m not sure as well
You don’t have any examples of where you done this by any chance? I figured how how to use the distance workflow in the Toolbox plugin firing off a button, but a bit uncertain how to hook it up to my RG?
Ok, that’s even better! That’s exactly what I have in my code at the moment - but I was worried it would consume API calls.
It makes sense if it doesn’t - I would assume under the hood Bubble is just using the same formula for straight line distance that Prashant provided. I will double check this in the next few days with some testing.