0

I am writing an application using phonegap to store an update lat/lon every 5 seconds to a mysql database. I would like to be able to allow my users to see the total distance traveled since starting the app.

I've taken a look at the Phonegap geolocation API and cannot see a way to calculate total distance traveled based upon lat / lon updates. Is there a way to accomplish this?

EDIT: @ Drew thanks for the link. I have looked it over and the JS version of Haversine looks straight forward. the difficult part will be the way phonegap pulls and stores lat/lon. Currently my function to get and send the location to MySQL is

function geo_success(position) {
    $("#status p").text("Tracking active");
    $('#status').removeClass("stopped").addClass("active");
    $('button').text("Stop tracking");

    latlon.lat = position.coords.latitude;
    latlon.lon = position.coords.longitude;
    latlon.alt = position.coords.altitude;
    if(!position.coords.speed) { latlon.speed = 0; }
    else{ latlon.speed = position.coords.speed }

    if(first) {
        intervalId = setInterval(send, 5000);
    }
    first = false;
}

Is there a way you can think of to store the latest value for lat1 lon1 and use the previous for lat2 lon2 and cycle the newest incoming coordinates through those 2 sets of variables? That way i can take the returned variable d from the haversine and store it in the db (to be able to sum it up later). Many thanks.

brettwmc
  • 470
  • 3
  • 10

1 Answers1

0

You would have to create an algorithm yourself that take those coordinates every 5 seconds, do some algebra on them to determine the distance between the two, and add it to the total distance somewhere, then repeat for the next 5 seconds.

For the actual algorithm of calculating the distance, look at this answer.

Community
  • 1
  • 1
Drew B.
  • 1,145
  • 10
  • 15