I have a project for which I need to track a car on the road. I have a GPS device in the vehicle and I get the coordinates from the device but the GPS location accuracy is not very good (< 10 meters). How can I snap the marker to the nearest road?

I have a project for which I need to track a car on the road. I have a GPS device in the vehicle and I get the coordinates from the device but the GPS location accuracy is not very good (< 10 meters). How can I snap the marker to the nearest road?

The Google Maps Tracks API offers functionality geared towards vehicle/asset tracking. For your particular use case, you can record crumbs (and even specify the precision of your measurement!) from your data source and then retrieve the collected crumbs snapped to the nearest road.
To only get the current position, just choose a suitable range for minTimestamp and maxTimestamp. If you'd like to avoid storing a history altogether, you should be able to just reuse a fixed timestamp:
If a crumb's timestamp is the same as an already-existing crumb for the specified entity, the existing crumb will be overwritten with the new one. (see "Recording crumbs")
Use the directions service. Get driving directions from the point of interest to the point of interest. Should be on the road.
var directionsService = new google.maps.DirectionsService();
directionsService.route({origin:homeLatlng, destination:homeLatlng, travelMode: google.maps.DirectionsTravelMode.DRIVING}, function(response, status) {
if (status == google.maps.DirectionsStatus.OK)
{
var homeMarker = new google.maps.Marker({
position: response.routes[0].legs[0].start_location,
map: map,
vtitle: "Check this cool location",
icon: image,
shadow: shadow
});
} else {
var homeMarker = new google.maps.Marker({
position: homeLatlng,
map: map,
title: "Check this cool location",
icon: image,
shadow: shadow
});
}
});
I suggest using OSM (OpenStreet Maps) data for getting offline routes, which means that You can snap marker to road without using data and without limitations to Google. Newest maps are also downloadable every week: search for planet.osm
I am using GraphHopper, which uses OSM, for my android project to get snapping to road, it is not perfect, because it has more roads that Goolge Maps and it is possible that Marker will be placed on road which Google Maps doesnt have.
So basically, check GraphHopper, download a sample project, download map, read instructions on GitHub Readme how to set project up and use their routing calculation to retrieve coordinates to closest road, without drawing marker or polyline. Retrieve coordinates and pass them to Your application and place marker on received coordinates.