为什么SphericalUtil.computeDistanceBetween方法不能返回准确的结果?

时间:2017-07-25 15:16:46

标签: android google-maps google-maps-android-api-2

您好我正在计算距离任何路线最近点的距离。我正在测试2条路线(折线)。路线相对于原点的最近点几乎在相同的坐标或点上,但是Google地图android库的SphericalUtil.computeDistanceBetween方法返回的结果却截然不同。

根据我的代码示例:

  for(Route route : routes){

        boolean isLocationOnEdge = PolyUtil.isLocationOnEdge(location, route.getLatLngCoordinates(), true, ROUTE_SEARCH_RADIUS);
        boolean isLocationOnPath = PolyUtil.isLocationOnPath(location, route.getLatLngCoordinates(), true,  ROUTE_SEARCH_RADIUS);

        if(isLocationOnEdge || isLocationOnPath){

            //meaning this route is near at the and compute distance from origin
            LatLng nearestPoint = findNearestPoint(location, route.getLatLngCoordinates());
            double distance = SphericalUtil.computeDistanceBetween(location, nearestPoint);
            Log.i(TAG, "origin: "+location);
            Log.i(TAG, "nearest point: "+nearestPoint); 
            Log.i(TAG, "distance: "+distance);
            route.setDistanceFromOrigin(distance);
            route.setNearestPointFromOrigin(nearestPoint);

            nearRoutes.add(route);
        }
    }

日志中的结果如下:

origin: lat/lng: (7.0792276,125.6248482)
nearest point: lat/lng: (7.079228237884086,125.6248559529709)
distance: 0.8584555298849629
origin: lat/lng: (7.0792276,125.6248482)
nearest point: lat/lng: (7.0792199725865546,125.62475406260717)
distance: 10.422383454451605

正如您所看到的,即使它们具有几乎相同的最近点,但两个距离之间存在很大差异。 (请参阅下面强调的比较)

路线A

origin:lat / lng:( 7.0792276,125.6248482

最近点:纬度/经度:( 7.079228237884086,125.6248559529709

距离: 0.8584555298849629

路线B

origin:lat / lng:( 7.0792276,125.6248482

最近点:lat / lng :( 7.0792199725865546,125.62475406260717

距离: 10.422383454451605

为了说明我绘制了多边形/路线及其最近点与原点。我突出显示并指示距离基于logcat中的代码记录的结果。与路线B 的距离(绿线)相比,为什么距离路线A 最接近原点的距离(红线)仅为 0.8584555298849629 > 10.422383454451605 ?根据最近点的位置和坐标,它们彼此靠近,因此它们的距离不应有很大差异。

enter image description here

这对我来说有点混乱。我错过了什么或者我的代码有问题吗?

1 个答案:

答案 0 :(得分:1)

没有证据表明SphericalUtil正在返回不准确的信息。

此地图描绘了您提到的原点和两点:

enter image description here

您可以在图像底部看到地图的比例。如果那么多像素是5英尺,那么两个“最近”的点似乎相距大约11米,这正是你的程序输出所说的。

相关问题