How to project the journey onto the street?
- The black polyline is inaccurate GPS data received from a mobile device, which positions the user trajectory inside the buildings, when it was actually on the road.
- The gray square pin is a sample inaccurate position at 59.436543,24.742133
- The green circled pin is the origin at 59.4367469,24.742495
- The green starred pin is the destination at 59.436377,24.742235
- The red crossed pin is what I would like to calculate as the more accurate position estimate?
here's the geojson data pictured above
What's the formula?
One way to get X would be to draw a straight line between the origin and the destination (the green line) and then draw a perpendicular line to the inaccurate position from that green line.. but I have no idea what algorithm is needed to implement that?
function align(inaccurLat, inaccurLon, originLat, originLon, destinLat, destinLon) {
// ???
return [alignedLat, alignedLon];
}
- What code would return the coordinates of X using the known coordinates of the circled pin, the starred pin and the square pin?
- The calculation should work regardless of where the inaccurate square position is: it might be on the left of the green line, but it might also be on the right of the green line, or in fact above the green line (above the origin).
- The function would ideally be in JavaScript, but I could do the translation to JavaScript myself if needed.
- It should work offline.
Looking for a simple map matching algorithm
Furthermore, I can draw many green lines, because I have many origin and destination pairs available to me as the reference points representing road segments that do not curve too much - as depicted in the image below: