据我所知,使用 Google Maps API 无法完成此操作。
我认为你最好的选择是使用众包数据集,例如开放街道地图 (OSM) http://www.openstreetmap.org/.
您需要建立自己的空间数据库(例如,PostGIS http://postgis.refractions.net/)并将OSM数据导入数据库。
然后,您将创建一个服务器端 API(托管在 Web 服务器中,例如Tomcat http://tomcat.apache.org/download-60.cgi or 玻璃鱼 http://glassfish.java.net/)接收手机当前位置,以一定半径缓冲该位置,得到一个圆形多边形,然后做通过PostGIS进行空间查询 http://www.bostongis.com/postgis_intersection_intersects.snippet确定缓冲区是否与任何道路相交(例如,带有“highway=primary”或“highway=secondary”标签的道路,具体取决于您要包含的道路类型 - 请参阅这个网站 http://wiki.openstreetmap.org/wiki/Highway),并将正确或错误的响应返回给手机。
2015 年 8 月编辑
现在有一个方法android-maps-utils 库 https://github.com/googlemaps/android-maps-utils called PolyUtil.isLocationOnPath()
假设您有构成道路(或任何其他线)的点集,那么您就可以在 Android 应用程序本身中进行此类计算。
这是代码内容看起来像在图书馆 https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/PolyUtil.java#L146:
/**
* 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);
}
要使用该库,您需要将该库添加到您的build.gradle
:
dependencies {
compile 'com.google.maps.android:android-maps-utils:0.4+'
}
然后,当您有了自己的点和路径后,您需要将纬度/经度转换为LatLng https://developers.google.com/android/reference/com/google/android/gms/maps/model/LatLng对象(具体来说,一个LatLng point
and List<LatLng> line
分别),然后在您的代码中调用:
double tolerance = 10; // meters
boolean isLocationOnPath = PolyUtil.isLocationOnPath(point, line, true, tolerance);
...查看您的点是否在您的线的 10 米范围内。
See the 入门指南 http://googlemaps.github.io/android-maps-utils/#start有关如何使用该库的更多信息。