I've been searching this SE site for quite a few hours now, and I'm still struggling to find a solution to my question. My goal is that given a way in OSM and my location (lat/lon coordinates), I want to find the closest location (lat/lon coordinates) on that way. The point can be anywhere on the way, not limited to the points used to define the way.
So I'm thinking of the following algorithm:
- Separate Path into separate edges, each edge connecting only two points.
- Select the closest edge.
- Project my location onto that edge.
Now there are many questions about calculating the distance between a location and a path:
- WGS point to WGS line segment (great circle) distance
- Calculating the distance between a point and a virtual line of two lat/lngs
- How to approximate point to segment distance on sphere?
Also a very similar question of which I cannot get the calculations right or verified:
There is also some info from Dr. Math about that subject. However I cannot seem to find an algorithm to calculate the location in step 3. As I haven't touched (vector) algebra in quite a while, I don't quite understand the logic in those answers.
Can someone show an algorithm to do this? A solution in any reasonable programming language is fine with me.