Fredrik Jonsson
Fredrik Jonsson

Reputation: 61

Is gps location between two locations

I'm working on a project that will track a vehicle as it drives around. The location of the vehicle is taken from the Geoloqi API and showed on a map that automatically is updated. The coordinates is passed to an AHAH function in Drupal that will load some more info about position via some other API:s. One of them is Geonames.org.

I do not want to load data from remote servers too often as this will increase load and use our credits too fast. However. I do want it to be as real time as possible as this is essential to the project. So I thought of doing it like this:

Geonames gives a string of GPS locations for the current street so that it can be drawn on a map. The "line" is given to the next intersection. We would eat our tickets too fast if we asked for current street name every time the location changed but how about at every intersection?

Would it be possible to, in PHP, check if a given gps coordinate is at the line between two other coordinates? It would also be good if this line could be extended a few meters in all directions in order to clear any offset.

Thank you so much for all suggestions.

Upvotes: 4

Views: 547

Answers (1)

Aleks G
Aleks G

Reputation: 57346

Your original question comes down to a simple math problem: find an equation of a line given two points that it passes. This part is simple: say, you have two points with (lat1, lon1) and (lat2, lon2) coordinates, then the equation of the line will be

(lon2 - lon1) * X + (lat1 - lat2) * Y + (lat1 * lon2 + lat2 * lon1) = 0

Now, when you get any other point (lat, lon), just put those coordinates as (X, Y) respectively into the equation. If you get 0, then the point is on the line. Then check that your lat is between lat1 and lat2 and your lon is between lon1 and lon2. If yes, then the new point is on the line between the two existing points.

The more complicated part comes from two things:

  1. GPS error. To account for that, you can do some approximation, for example, instead of comparing to 0, give it some leeway, e.g. compare from -err to +err where err is some value that you'd define.

  2. The fact that most streets are not straight. The picture below shows the problem. If you look at the line between the two ends of the top street, the top of the vertical street will be on that line - but that's not what you want.
    enter image description here.

Maybe a better approach would be to use a distance change. Do not re-query position if the GPS-reported distance changed by less than some predefined value.

Upvotes: 2

Related Questions