Calculating the distance between two sets of coordinates on Thunkable


Calculating the distance between two sets of map coordinates can be very useful, whether you're trying to make your own Pokemon GO game, or you just want to calculate the distance to the nearest Pizza shop, this guide will show you a couple methods to calculate the distance in Thunkable.

While I figured out a solution to this problem I have realised that there are now two methods of doing this, the first method uses some complicated mathematics to calculate the distance between the two coordinates, and the second method makes use of the Google Maps Distance Matrix API's to do all the heavy lifting.

I will be going over the first method and highlighting the pros and cons for each method.

Method 1: 

When I first started looking for a solution to this problem I come across the following stack overflow forum where someone very clever had posted a javascript method of calculating the distance between two sets of latitude and longitude coordinate using only math! the following is the javascript.

function measure(lat1, lon1, lat2, lon2){  // generally used geo measurement function
    var R = 6378.137; // Radius of earth in KM
    var dLat = lat2 * Math.PI / 180 - lat1 * Math.PI / 180;
    var dLon = lon2 * Math.PI / 180 - lon1 * Math.PI / 180;
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
    Math.sin(dLon/2) * Math.sin(dLon/2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    var d = R * c;
    return d * 1000; // meters
}
http://stackoverflow.com/questions/639695/how-to-convert-latitude-or-longitude-to-meters

Looks pretty complicated eh? well it is certainly complicated and my brain has no way of understanding what it all does but I tried it anyway and it worked! the result of using this method in Thunkable is a lot of code blocks, but it works without any external web request and the math is done locally on the device.


Method 1: Advantages vs Disadvantages
Advantages Disadvantages
-Works offline without the internet -Only calculates distance directly from point A to point B, can't use this to calculate a driving route distance
-Doesn't require API calls -Requires a lot of blocks in the procedure which takes up a lot of space and time


This is what the code looks like in the Thunkable  blocks view:
I have also created a demo application that you can try which uses this same code in a little example that draws a line from your location to my home city Adelaide, and gives a readout at the top telling you how many Kilometres you are away from here, you can also get the distance in metres by multiplying the result by 1000.

You can download the project here: CalculateDistanceExample.aia

Method 2:

A moderator for Thunkable has put together a script that makes use of the Google Maps Matrix Distance API which allows you to make use of a variety of variables when calculating the distance between two sets of coordinates. 

By using Google's Distance Matrix API you can calculate the distance along a driving route for example, instead of directly from point A to point B, giving you a more accurate estimate of distance, this also depends on the needs of your app, this may not be necessary for you.

Using this script is a little more complicated because you have to build the URL and then make the request but it gives you a lot more options to customise how the result is calculated.

To learn more about this method visit the Thunkable Community post Google Maps Distance Matrix.


Method 2: Advantages vs Disadvantages
Advantages Disadvantages
-Provides a vast array of options for customising how the distance output is calculated -Requires an internet connection to be able to calculate the distance
-Less space is taken up in the Thunkable blocks view -The various options and URL building can be tricky to understand

Was this helpful?

Yes No

Comments

Thunkable Components