Computing the great circle distance of a device

Rimbalza5 years ago

Background: I was searching for a way to have an answer to the question "How much time will the device (on a truck) take to come home?".
This is what I concluded: I need to calculate the distance between the current position of the truck and the home depot (fixed coordinate point).
That is the direct distance, we need to multiply this by a constant to simulate the real road path (say x1.5).
The problem is to have a calculated attribute with the distance: I used the Haversine Formula (read about its limitations) and tried to create a Jexl version. After a lot of experimenting here is the formula ready to use:

6373 * (2 * math:atan2( math:sqrt(((math:sin((MYLATITUDE - latitude)/2))*(math:sin((MYLATITUDE - latitude)/2)) + math:cos(latitude) * math:cos(MYLATITUDE) * (math:sin((MYLONGITUDE - longitude)/2))*(math:sin((MYLONGITUDE - longitude)/2)))), math:sqrt(1-((math:sin((MYLATITUDE - latitude)/2))*(math:sin((MYLATITUDE - latitude)/2)) + math:cos(latitude) * math:cos(MYLATITUDE) * (math:sin((MYLONGITUDE - longitude)/2))*(math:sin((MYLONGITUDE - longitude)/2))))))

You need to replace MYLATITUDE and MYLONGITUDE with the coordinates of the fixed point to calculate the distance from.
You'll obtain the distance in meters.
In this way you can divide that for the average speed of your truck and you know (approximately) how much it takes for your truck to get back home, or its distance in minutes/hours.

Hope this helps others having the same need.

Rimbalza5 years ago

I'm not able to edit my post above, here is a better formula (result is in km)

(math:round(6371 * 2 * math:atan2(math:sqrt(((math:sin((math:toRadians(latitude - MYLATITUDE)) / 2) * math:sin((math:toRadians(latitude - MYLATITUDE)) / 2)) +
                    (math:cos(math:toRadians(latitude))) *
                    (math:cos(math:toRadians(MYLATITUDE))) *
                    (math:sin((math:toRadians(longitude - MYLONGITUDE)) / 2)) *
                    (math:sin((math:toRadians(longitude - MYLONGITUDE)) / 2)))), math:sqrt(1 - ((math:sin((math:toRadians(latitude - MYLATITUDE)) / 2) * math:sin((math:toRadians(latitude - MYLATITUDE)) / 2)) +
                    (math:cos(math:toRadians(latitude))) *
                    (math:cos(math:toRadians(MYLATITUDE))) *
                    (math:sin((math:toRadians(longitude - MYLONGITUDE)) / 2)) *
                    (math:sin((math:toRadians(longitude - MYLONGITUDE)) / 2)))))))