Probably this is not purely GIS related question, its related to developing issues. I am just asking if there is any kind of suggestions from Gurus. I am building an tracking application where GPS data is receiving after every 15 seconds. I am using Leaflet, PostGIS and OSM solution. The GPS longitude and latitude are not on the road every time. I have illustrated 3 cases of tracking route here.
Consider the following Figure One. Tracking route starts from Blue Marker (Marker 1) and ends at Red Marker (Marker 2). The actual tracking route should be the Blue line following the Yellow Major Highway Roads of Map. The green dots are the GPS positions which is receiving after every 15 seconds simultanously. If I simply draw a polyline according to the GPS points the tracking route will be the Green Line. As it is not the real route of vehicle there are so many disadvantages during routing and distance analysis.
Now Consider the following Figure Two for case two. Here I have optimize the GPS positions and calculate a nearest locations of GPS points on road. The Red dots are the false positions of GPS data. If we see deeply the second, fourth and fifth points are not in right position. Considering these three points, they were calculated and plotted from nearest road but not on the right position. They should be on Blue Line. In most road junction this can be happened. If I draw a tracking polyline it should be same as Red Polyline of Figure two. This returns better result than case one but not solving the issue accurately.
Now consider the following Figure Three where I have implemented both false position of GPS data and Routing. Its return more better result than case one or two but not solving the issue exactly. If we see deeply the routing result not showing accurate tracking path due to those three wrong false points and the result returns same as Gray Polyline of Figure three. On second point its going to a local road and again returns to the major highway. For the fourth and fifth position its following another major highway avoiding the accurate vehicle path.
What will be the best solution to get the accurate tracking path. What will be the best optimization in this case. I want to build up a solution similar to the following example of instacart site where vehicle icon is following the route and shows with an animation that show vehicle icon moving forward on road.