安卓 - 如何确定坐标是否在谷歌地图上的道路上

5
我需要在我的应用程序中进行检查,确定给定的坐标是否在Google Maps上的道路上。
Google Maps API 中是否有任何函数可以帮助我完成这项任务?
提前致谢!

1个回答

15
据我所知,使用Google Maps API无法完成此操作。
我认为最好的选择是使用众包数据集,例如OpenStreetMap (OSM)
您需要设置自己的空间数据库(例如PostGIS),并将OSM数据导入到数据库中。
然后,您需要创建一个服务器端API(托管在Web服务器中,例如TomcatGlassfish),以接收移动电话的当前位置,使用一定半径缓冲该位置以给您圆形多边形,并通过PostGIS空间查询进行空间查询以确定缓冲区是否与任何道路相交(例如,具有标签“highway = primary”或“highway = secondary”的道路,具体取决于您想要包括哪些道路类型-请参见此网站),并将真实或虚假的响应返回给手机。

编辑于2015年8月

现在android-maps-utils库中有一个方法叫做PolyUtil.isLocationOnPath(),它可以让你在Android应用程序内部进行这种计算,假设你有组成道路(或任何其他线路)的点集。

这是库中代码的样子

   /**
     * 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对象(具体来说,是LatLng pointList<LatLng> line),然后在您的代码中调用:

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

检查你的点是否在你的线的10米范围内。

有关如何使用此库的更多信息,请参阅入门指南


MapUtils的Pro-guard设置? - Salman Khakwani
不应该有任何区别,与使用支持库和Google Play服务的其他项目相同-例如,请参见https://github.com/CUTR-at-USF/OpenTripPlanner-for-Android/blob/master/opentripplanner-android/proguard.cfg - Sean Barbeau

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接