Android - How to determine if coordinates are on the road in Google Maps

I need to check in my application, which determines whether these coordinates are on the road or not in Google Maps.

Is there any feature in the Google Maps API that can help me?

Thanks in advance!

+4
source share
1 answer

As far as I know, this cannot be done using the Google Maps API.

I think your best bet is to use a crowd-generated dataset like OpenStreetMap (OSM) .

You will need to configure your own spatial database (e.g. PostGIS ) and import the OSM data into the database.

You will then create a server API (hosted on a web server such as Tomcat or Glassfish ) to get the current location of the mobile phone, buffer the location with a specific radius to give you a round polygon, and do a spatial query through PostGIS to determine if the buffer intersects any roads (for example, paths labeled "highway = primary" or "highway = secondary", depending on what types of roads you want to include - see this site ) and return a true or false answer to the phone.

EDIT Aug 2015

Now the android-maps-utils library now has the PolyUtil.isLocationOnPath() method, which will allow you to perform this type of calculation inside your Android application, assuming that you have a set of points that make up the road (or any other line).

Here the code looks 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 need to add the library to your build.gradle :

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

Then, when you have a point and your path, you will need to convert your lat / longs to LatLng objects (in particular, 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 find out if your point is within 10 meters of your line.

For more information on how to use it, see the Getting Started Guide for this library.

+9
source

Source: https://habr.com/ru/post/1442480/


All Articles