Android - 如何确定Google地图上的坐标是否在路上 [英] Android - How to determine whether coordinates lie on road in Google Maps

查看:118
本文介绍了Android - 如何确定Google地图上的坐标是否在路上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的应用程序中进行检查,以确定给定的坐标是否在Google地图上行进。

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

Google Maps API中是否有任何功能这可以帮助我吗?

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

预先感谢!

Thanks in advance!

推荐答案

据我所知,使用Google Maps API无法完成这项工作。

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

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

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

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

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

然后,您将创建一个服务器端API(托管在Web服务器中,如 Tomcat Glassfish )接收手机的当前位置打开,缓存具有一定半径的位置,为您提供一个圆形多边形,并通过PostGIS进行空间查询

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.

编辑2015年8月

现在 android-maps-utils库,名为 PolyUtil.isLocationOnPath(),可以让您在Android中执行此类计算应用程序本身,假设你有一组构成道路(或任何其他线路)的点。

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).

以下是代码在库中看起来像: p>

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);
    }

要使用这个库,您需要将库添加到 build.gradle

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+'
}

然后,当你有你的观点和路径时,你需要将你的纬度/经度转换为 LatLng 对象(特别是 LatLng point code> List< LatLng>行),然后在您的代码调用中:

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);

...查看您的观点是否在线路10米内。

...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.

这篇关于Android - 如何确定Google地图上的坐标是否在路上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆