GPS data representation

I am working on a GPS with PHP and Leaflet (JS) that works by creating a route between the points I have in the database. The GPS works usually with high precision but the problem is that the points sometimes get out of the road a little bit.

I want to ask if your know a better solution for this kind of case, preferring the use of Leaflet for that, likewise, I’m open to other solutions.

Thanks,
Guille