How do you count the distance traveled using gps?

Hi. I'm trying to build an application that will show me how many meters I have traveled. I'm thinking of counting the distance between two coordinates every 5 seconds. However, I don't know how to implement it in the form of blocks.
Or maybe you have another idea how to calculate the distance traveled?

Distance traveled or distance between two points Adrian? They are different; see

and

Hi SteveJG. Distance traveled. For example. I'm driving a car and cliking on start button which will start counting distance traveled. So for every 5 or 10 second i will receive updated distance in meters which i traveled from point when start button was activated.
If it matters i'm using KIO4_LocationSensor1 extension in my app.

  • use Navigate control

  • request distance to destination using GotDirections(directions,points,distance,duration) .

  • post distance in a Label perhaps

  • request directions again using a Clock event handler, set Clock interval to perhaps 30 seconds (5 to 10 is too often) and call GotDirections distance to destination (enrouteDistance)

might work for you. See one of the example links which describe something similar.

I'm fighting with that from few hours but with any result. I don't exactly how to build these blocks. If it will make things easier i want to count distance in just straith line.
My blocks below:
image

This example might help

or this one

See post number 2 and use a Great Circle algorithm for kilometers or miles

Instead of counting the distance every 5 seconds, consider using the Haversine formula to calculate the total distance traveled between two points (initial and final). This formula accounts for the Earth's curvature and provides a more accurate measurement than straight-line distance.

You can implement this formula using blocks by first storing the initial coordinates and then periodically (e.g., every 5 seconds) calculating the distance between the current and initial coordinates using the Haversine formula. Finally, sum up all these individual distances to get the total traveled distance.

How can we do that in blocks, ie using Haversine Formula?

this is how to solve this question in my way:

  1. search "Haversine Formula javascript",
  2. you will find a function of that in javascript, like this:
    got below code from here:
    Remember to remove the comments, if there are // in javascript, the RunJavascript function will not work.
let computedDistance = function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
        Number.prototype.deg2rad = function (deg) {
            return deg * (Math.PI / 180)
        };
        var R = 6371; 
        var dLat = 0..deg2rad(lat2 - lat1);  
        var dLon = 0..deg2rad(lon2 - lon1);
        var a =
            Math.sin(dLat / 2) * Math.sin(dLat / 2) +
            Math.cos(0..deg2rad(lat1)) * Math.cos(0..deg2rad(lat2)) *
            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;
    };
                       

  1. use Webviewer.Runjavascript to get a result:
    photos draggable.
    blocks (2)

1 Like

Il try to grasp this.

Using App Inventor Blocks as already posted in the thread:

1 Like

Il try this.

You can also use the Marker component. You can compute distance between any two Map-based components using the appropriate functions, and these use a built-in implementation that should be much faster than implementing it in blocks.

1 Like

@SteveJG , Yesterday when I checked, I saw here about using haversine formula using mit blocks but today I am not able to find it.

My purpose is to know when a vehichle started to move (begining time), so the app will give some warning message as 'Drive safely or wear seat belt etc'.

What about following the link, @SteveJG posted earlier?
And what about trying the tip from @ewpatton ?

Taifun

1 Like