I am a new member. For school, they want us to write code to figure out the speed of the device (when driving). Is this possible in javascript? Im assuming it will be challenging but any ideas help! Thank you in advance!
Asked
Active
Viewed 305 times
0
-
1There's a location API that you should be able to use to get the user's GPS coords. Use that + Date() you can calculate the speed. The user will have to consent to location access and will probably need to keep the webpage open for you to poll their coords. Oh...and to Klaycon's point... I was assuming a browser, but this will be different if you're running Node or Deno or something. See https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition – mpen Jun 02 '20 at 21:19
-
Hypothetically, if the browser has access to geolocation, then by getting the timestamp and the location, one can calculate haversine_distance between the two locations, which, divide by the time, should give you speed. As for how, I leave that up to you. – Kasey Chang Jun 02 '20 at 21:21
1 Answers
2
You can use Geolocation API to get current position.
Since speed is distance over time, you can use a setInterval() to periodically get the position (every 3 seconds for example) and calculate the distance between the last and current coordinates.
Since that's a homework/school work, I think I shouldn't give you any code.

Elias Soares
- 9,884
- 4
- 29
- 59