gpt4 book ai didi

Android - 如何确定坐标是否位于谷歌地图中的道路上

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:57:01 24 4
gpt4 key购买 nike

我需要在我的应用程序中进行检查,以确定给定坐标是否位于 Google map 中的道路上。

Google Maps API 中是否有任何功能可以帮助我做到这一点?

提前致谢!

最佳答案

据我所知,使用 Google Maps API 无法做到这一点。

我认为最好的选择是使用众包数据集,例如 OpenStreetMap (OSM) .

您需要设置自己的空间数据库(例如 PostGIS)并将 OSM 数据导入数据库。

然后,您将创建一个服务器端 API(托管在网络服务器中,例如 TomcatGlassfish )以接收手机的当前位置,以一定半径缓冲该位置以提供给您一个循环多边形,然后做一个 spatial query via PostGIS确定缓冲区是否与任何道路相交(例如,标签为“highway=primary”或“highway=secondary”的道路,具体取决于您要包括的道路类型 - 请参阅 this site ),并返回 true 或 false 响应到电话。

编辑 2015 年 8 月

android-maps-utils library 中现在有一个方法称为 PolyUtil.isLocationOnPath()假设您拥有构成道路(或任何其他线)的一组点,这将允许您在您的 Android 应用程序本身内进行此类计算。

这是代码 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);
}

要使用该库,您需要将该库添加到您的 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 米范围内。

参见 Getting Started Guide对于这个库,了解如何使用它的更多信息。

关于Android - 如何确定坐标是否位于谷歌地图中的道路上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13100412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com