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
});
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With