I am currently in the process of writing a web app that uses the Google Maps API and the Chicago Transportation Authority API to display the location of the L trains in real time. In lieu of calling the train tracker API every 1 or 2 seconds, I decided that I would come up with a set of algorithms that could essentially guess the train’s path with a minimal number of API calls. However, I have run into an obstacle, and I don’t know how exactly to calculate the speed. So far, I have the following:

Algorithm

[JavaScript] Speed Algorithm - Pastebin.com

Algorithm Play by play

- The train tracker API is called 3 times in 9 seconds (once per 3 seconds)
- The distance between points ‘A’ and ‘B’ are calculated
- The distance between points ‘B’ and ‘C’ are calculated
- The time between the collection of point ‘A’ and the collection of point ‘B’ is calculated (this is to account for any data transfer times, etc)
- The time between the collection of point ‘B’ and the collection of point ‘C’ is calculated
- The average distance traveled between points is calculated
- The average travel time between points is calculated

With this information, I can come up with: “The train traveled a total of <average distance> units in a time of <average time> seconds”. After that, I need to make that statement into an MPH value, which is the step in which I am hitting a dead end. Any help would be greatly appreciated. Thanks ahead of time!