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.
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
-
@JSuar exactly as Mr.Johnson stated. – jason Jan 04 '14 at 16:58
-
Sorry about that. Updated my answer. If none of these links prove useful, I will delete my answer. Still researching, however. – JSuar Jan 04 '14 at 17:00
-
@JSuar Thanks again for your time and effort. – jason Jan 04 '14 at 17:02
-
http://econym.org.uk/gmap/example_snap.htm snaps to nearest road how does it know where the road is ? Its interesting .Can we implement the same on android google maps v2 ?Let me know any workaround.Thanks again for your time. – jason Jan 04 '14 at 17:30
-
@jason That solution relies on [loadFromWaypoints](https://developers.google.com/maps/documentation/javascript/v2/reference?csw=1#GDirections.loadFromWaypoints). [This SO question provides a solution](http://stackoverflow.com/questions/8271176/loadfromwaypoints-with-more-than-25-points-using-google-maps-api-v2) to the restrictions placed on the function. – JSuar Jan 04 '14 at 17:37
-
40.721171, -74.011178 is the point I have clicked how do I snap to nearest road ? in other words how do I know coordinates or polylines for the nearest road ? – jason Jan 04 '14 at 17:54
-
The links in my answer and previous comment provide code examples. I'd just be reposting their code. Unless you post some of your own code, I can't suggest what exactly you need to do. – JSuar Jan 04 '14 at 18:28
-
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.721171,%20-74.011178&sensor=false .How do I know which is the nearest street? or how do I point it to nearest road?Sorry for not providing more code but I have exact location w.r.t to google maps.Please let me know if you need more info. – jason Jan 04 '14 at 18:45
-
how do I snap to nearest road from here . http://maps.googleapis.com/maps/api/geocode/json?latlng=40.721171,%20-74.011178&sensor=false – jason Jan 04 '14 at 18:56
-
[This documentation](https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding) explains how to interpret and parse the results you need. I know the final pieces are fuzzy but this looks like it will solve your problem. Pretty cool that this geocoding service is available. – JSuar Jan 04 '14 at 20:46
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44520/discussion-between-jason-and-jsuar) – jason Jan 04 '14 at 20:48
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

- 13,583
- 5
- 53
- 82