我需要在我的应用程序中进行检查,确定给定的坐标是否在Google Maps上的道路上。
Google Maps API 中是否有任何函数可以帮助我完成这项任务?
提前致谢!
Google Maps API 中是否有任何函数可以帮助我完成这项任务?
提前致谢!
编辑于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 point
和List<LatLng> line
),然后在您的代码中调用:
double tolerance = 10; // meters
boolean isLocationOnPath = PolyUtil.isLocationOnPath(point, line, true, tolerance);
检查你的点是否在你的线的10米范围内。
有关如何使用此库的更多信息,请参阅入门指南。