Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to determine whether coordinates lie on road in Google Maps

I need to make a check in my application that determines whether the given coordinates lie on road or not in Google Maps.

Is there any function in Google Maps API that can aid me with that?

Thanks in advance!

like image 665
MrByte Avatar asked Oct 27 '12 13:10

MrByte


1 Answers

As far as I know this can't be done using the Google Maps API.

I think your best bet is to use a crowd-sourced dataset such as OpenStreetMap (OSM).

You'd need to set up your own spatial database (e.g., PostGIS) and import OSM data into the database.

Then, you'd create a server-side API (hosted in a web server such as Tomcat or Glassfish) to receive the mobile phone's current location, buffer the location with a certain radius to give you a circular polygon, and do a spatial query via PostGIS to determine if the buffer intersects any roads (e.g., ways with labels of "highway=primary" or "highway=secondary", depending on what road types you want to include - see this site), and return the true or false response to the phone.

EDIT Aug 2015

There is now a method in the android-maps-utils library called PolyUtil.isLocationOnPath() that would allow you to do this type of calculation within your Android app itself, assuming you have the set of points that make up the road (or any other line).

Here's what the code looks like in the library:

   /**
     * Computes whether the given point lies on or near a polyline, within a specified
     * tolerance in meters. The polyline is composed of great circle segments if geodesic
     * is true, and of Rhumb segments otherwise. The polyline is not closed -- the closing
     * segment between the first point and the last point is not included.
     */
    public static boolean isLocationOnPath(LatLng point, List<LatLng> polyline,
                                           boolean geodesic, double tolerance) {
        return isLocationOnEdgeOrPath(point, polyline, false, geodesic, tolerance);
    }

To use this library, you'll need to add the library to your build.gradle:

dependencies {
    compile 'com.google.maps.android:android-maps-utils:0.4+'
}

Then, when you have your point and your path, you'd need to convert your lat/longs to LatLng objects (specifically, a LatLng point and List<LatLng> line, respectively), and then in your code call:

double tolerance = 10; // meters
boolean isLocationOnPath = PolyUtil.isLocationOnPath(point, line, true, tolerance);

...to see if your point is within 10 meters of your line.

See the Getting Started Guide for this library for more info on how to use it.

like image 165
Sean Barbeau Avatar answered Oct 01 '22 05:10

Sean Barbeau