Calculate if position is on direct line between two points

Former Member
Former Member
Hello together, I'm currently developing an app in which I need to calculate some values based on GPS coordinates. One thing I struggle to get working is the following: I have two coordinates that have a virtual line between them. What I want to calculate now is how far I am away from the closest point on that line.
  • Former Member
    Former Member over 9 years ago
    May be this can help you
    http://www.movable-type.co.uk/scripts/latlong.html


    This site really did help me a lot with my app so far, but does not have any answers for me regarding this specific problem. Nevertheless, thanks for your effort.
  • Pythagoras...

    Can't you use Pythagoras?

    You know 2 points (start, end) and where you are currently...

    You can work out where on the line you *would* be (if you were actually on the line)...

    Can you use this to calculate your deviation from the line?
  • Assuming the start, end, and current position are from GPS, one thing to bear in mind is that GPS can be off a bit, so you may want to do this so that it's not on a line, but instead near the line, with a margin of error.

    I have an app where I save off GPS locations and actually show this kind of thing graphically, On the screen shot, the blue circle is where I started, the yellow circles are GPS locations I saved, and the green dot is my current location. If I want to go from one place to another, I just make sure my position follows the of the lines.
  • Former Member
    Former Member over 9 years ago
    Assuming the start, end, and current position are from GPS, one thing to bear in mind is that GPS can be off a bit, so you may want to do this so that it's not on a line, but instead near the line, with a margin of error.

    I have an app where I save off GPS locations and actually show this kind of thing graphically, On the screen shot, the blue circle is where I started, the yellow circles are GPS locations I saved, and the green dot is my current location. If I want to go from one place to another, I just make sure my position follows the of the lines.


    To have an error margin is a good hint! I will have that in mind as soon as I have a solution for my problem. To clarify what I mean I tried to visualize it. The black circles are saved locations. The blue circle is my current location. The green line is just for visualization (it is the shortest connection between the two save coordinates). The blue line is the shortest way to reach this connection. What I need to know is how do I get the coordinates of the point where the grenn line and the blue line cross.

  • The solution is on that page. It is in the section entitled Cross-track distance.
  • RMZ84 - to give you an idea on margin of error, in my app I vibrate the vivoactive when I get to a saved location, and with testing, I found that 75' was a reasonable distance for that. If it was less than that, you had to be too exact. Based on an Etrex where more detailed GPS data is available, I often see the GPS accuracy at about 30', thus the 75 (about the max error in current location+max error in save location)
  • Former Member
    Former Member over 9 years ago
    The solution is on that page. It is in the section entitled Cross-track distance.


    Perfect Travis. Thats it. Thank you a lot! I have a followup question on this. If I want to know the bearing I need to mov to get to the line on this shortest distance. How would I do that. My guess would be something like this:

    The shortest distance will allways be on an orthogonal line to the startline (the line from the previous question). So I can calculate the bearing by subtracting the bearing from point1 to point2 from 90 degrees and have my course to follow. Is that pointing in the right direction?
  • I have a followup question on this. If I want to know the bearing I need to mov to get to the line on this shortest distance....

    Can't you just calculate a new bearing using your existing code to calculate the bearing from the current position to the target?
  • Former Member
    Former Member over 9 years ago
    Can't you just calculate a new bearing using your existing code to calculate the bearing from the current position to the target?


    I'm missing one value for that. With the formula for Cross-track distance I got the distance from my current position but not the actual position on the line. To calculate that position I would need a bearing and to calculate a bearing I would need the position. At least if I use the given formulas on that site.