Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if lat long coordinates are on a road with Google Maps API v2

I have used Google geo address and using lat/long have got the address. How do I determine if I am on road on not? Basically hit-test for road? I am currently using location manager for lat/long and the GeoAddress is same for on road and next to road.

like image 958
jason Avatar asked Nov 29 '13 21:11

jason


2 Answers

Final Solution

Use reverse geocoding via the Google Geocoding API.

The term geocoding generally refers to translating a human-readable address into a location on a map. The process of doing the converse, translating a location on the map into a human-readable address, is known as reverse geocoding.

The Geocoding API supports reverse geocoding directly using the latlng parameter. For example, the following query contains the latitude/longitude value for a location in Brooklyn:

http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false

The docs explain how to interpret the json results but StackOverflow has various answers addressing more efficient ways to parse the data.

  • https://stackoverflow.com/a/8095967/1085891
  • https://stackoverflow.com/a/8314094/1085891
  • https://stackoverflow.com/a/6335080/1085891

Possible Solutions

  • Getting the nearest street/road coordinates in android - the answer to this question may point you in the right direction.

    You can use directions api directly: http://maps.googleapis.com/maps/api/directions/json?origin=51,19&destination=51,19&sensor=false

    Could you use the API to find directions which would then provide the closest road?

  • Stick position to road on android maps v2
  • GPSLocator - App to Find Current (Nearest) Location using GPS - possibly helpful; not so sure on this one.
  • Snap to Roads Android - Exact same question

Quick Search Results

  • Snap to nearest street
  • Maps API Blog using GDirections.loadFromWaypoints
  • Finding nearest street given a lat-long location
  • Driving route from my location to destination in Google Maps Android API V2

Unsolved Similar Questions

  • Trace the location only on Road in Google maps V2 android
  • https://stackoverflow.com/questions/18081417/google-maps-v2-snap-to-road-issue
like image 111
JSuar Avatar answered Oct 31 '22 18:10

JSuar


See fiddle.

It can be done so much easier with OSRM (nearest) webservice. Make a GET request like:

const url = '//router.project-osrm.org/nearest/v1/driving/';
fetch(url + coord.lon + ',' + coord.lat).then(function(response) { 
  return response.json();
}).then(function(json) {
  if (json.code === 'Ok') {
    console.info(json.waypoints[0].location);
  }
});

Which is a road/street coordinate.

References — Project-OSRM

like image 43
Jonatas Walker Avatar answered Oct 31 '22 18:10

Jonatas Walker