I'm developing a navigation app in android. I want to prompt the user when he goes off-road. So, if I have a GPS Lat/Lng point, is it possible to determine if that point lies on a road or not? Is there any API support for such checks? This is to be done on all roads in general and not while having a specified path/polyline.
Check if a GPS Lat/Lng point lies on a road in Google maps
Asked Answered
What does it mean to go off-road? The trouble is that roads are represented by lines and lines have no width. If the car has two wheels on the road and two wheels on the shoulder, is it off-road? You might want to consider a buffer of a certain size and then check if the point that represents the vehicle is within it. –
Nauseous
@Marcelo, Roads are represented by lines right? Then how to check whether a coordinate lies on that line or not? –
Cuff
Think about the geometry! if the line has no width then the point would have to be 100% on the line to give you a numerical match. if it was just 1 millimeter away you would not get a match, but 1 millimeter is insignificant for a car. You can round down the decimals and get closer, but by doing that you'd be actually considering a buffer as I suggested in my first comment. –
Nauseous
@Marcelo, I agree with the idea of buffer, but still how to check if its on a road line? –
Cuff
If you're using the buffer technique and a spatially enabled database, (like PostgreSQL/PostGIS), then you can use the built in geometry functions to perform the check on the server side and pass the result to your application. See for example the function ST_Within(geometry A, geometry B); here: postgis.refractions.net/documentation/manual-1.4/ST_Within.html - geometry A would be the point representing the vehicle and geometry B would be the buffered road. –
Nauseous
Thanks but, I don't think that will solve my problem. –
Cuff
Why not? Look at this mashup: gis5.com/pois_along_route/gm_pois_along_route.php - it is doing exactly that to decide if a restaurant is within the given distance from the road or not. You just need to make your buffer tighter, closer to the road. –
Nauseous
How to implement it on android? Can you give me a sample? –
Cuff
You can buld a webpage with the Google Maps javascript API, use a webview to display it and do all the hard work on your PostgreSQL/PostGIS server : developer.android.com/guide/webapps/webview.html or there may be a better way using a mapview instead, but I am not an expert on Android. –
Nauseous
Possible duplicate of Android - How to determine whether coordinates lie on road in Google Maps –
Ikkela
That question was posted after this one. –
Cuff
You can use the isLocationOnPath()
function for that. You have to add the dependency
dependencies {
compile 'com.google.maps.android:android-maps-utils:0.4+'
}
in your app gradle file. For more information http://googlemaps.github.io/android-maps-utils/#start
© 2022 - 2024 — McMap. All rights reserved.