How to calculate total distance traveled?

Is it possible to make an HTML5 Trip Meter that tracks distance traveled?

  • I'm trying to develop a webapp that users can run on their mobile phone to track the distance they've traveled. Think of it as something like RunKeeper, but in webapp form. The following article describes an easy way to create a Trip Meter with HTML5 and and the Geolocation API. http://www.html5rocks.com/en/tutorials/geolocation/trip_meter/ However, this just seems to calculate the distance between point A and point B. I suppose I could use something like JavaScript's setTimeout to track the distance traveled every 5 seconds, then calculate the total at the end. However, when the webapp is running in the background (or the phone is locked), this JS won't be executed. Is there any sort of solution to this problem?

  • Answer:

    You don't need to use setTimeout, the http://dev.w3.org/geo/api/spec-source.html#watch-position will fire the callback function every time the location is updated. However, you are correct that the JavaScript will only run when the web app is not in the background and the phone is not locked. You may be able to write an HTML5 app then convert it to native with something like http://docs.phonegap.com/en/1.0.0/phonegap_geolocation_geolocation.md.html.

Matt Raible at Stack Overflow Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.