3

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?

enter image description here

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
Scar
  • 3,460
  • 3
  • 26
  • 51

3 Answers3

7

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")

mensi
  • 9,580
  • 2
  • 34
  • 43
  • could you please provide some example code, I got your concept and it seems great, but how could I get the nearest road location from the timestamp? – Scar Apr 02 '14 at 18:39
  • See here: https://developers.google.com/maps/documentation/tracks/crumbs#identifying_roads_along_which_an_entity_traveled – lambshaanxy Apr 07 '14 at 02:23
  • 1
    Every link in your answer is 404. That API doesn't exist anymore. For 2023, if you're after a Google solution, you can use the [Roads API](https://developers.google.com/maps/documentation/roads/overview). – MrUpsidown Mar 15 '23 at 13:36
1

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
    });
      }
});
Sam Makin
  • 1,526
  • 8
  • 23
  • This solution is limited for 2500 request per day, which is invalid for my case, even if I use the business plan. – Scar Apr 01 '14 at 19:04
  • 1
    The business plan is capped at 100000 uses in 24 hours. You are going to have to pay something to make a massive amount of requests to this (or a similar) service per day. How many requests are you estimating? – Sam Makin Apr 02 '14 at 10:02
  • I'm building an iOS app that will be used from more than 1000 people and they will open it and every 10 sec. there will be a request for each user, so the number of request is huge. that why this way is not valid. – Scar Apr 02 '14 at 18:15
  • The 2500/100000/day limit applies only to the geocoding *web service*. The Javascript API, on the other hand, is virtually unlimited. http://stackoverflow.com/questions/20087969/google-javascript-api-geocoding-limits/20960779#20960779 – lambshaanxy Apr 07 '14 at 02:21
  • 1
    That said, the Tracks API in @mensi's answer is the best approach, it's meant exactly for this. – lambshaanxy Apr 07 '14 at 02:24
1

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.

dpitkevics
  • 1,238
  • 8
  • 12