为什么Android location.distance要以百万为单位返回值?

Two*_*cks 1 android android-maps android-maps-v2

在我的Android应用程序中,我试图计算两个位置之间的距离,但获得的值却是数以千万计的1100万以上。两点/位置之间的实际距离仅为1.1km-1.3Km。为什么会这样呢?即使.distanceTo方法返回的值以米为单位,1100万米仍然是一个很大的值。

这是我的代码:

        Location locationA = new Location("LocationA");
        locationA.setLatitude(lat);
        locationA.setLongitude(lang);

        Location locationB = new Location("LocationB");
        locationB.setLatitude(14.575224);
        locationB.setLongitude(121.042475);

        float distance = locationA.distanceTo(locationB);
        BigDecimal _bdDistance;
        _bdDistance = round(distance,2);
        String _strDistance = _bdDistance.toString();      

Toast.makeText(this, "distance between two locations = "+_strDistance, Toast.LENGTH_SHORT).show();

  public static BigDecimal round(float d, int decimalPlace) {
        BigDecimal bd = new BigDecimal(Float.toString(d));
        bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
        return bd;
    }
Run Code Online (Sandbox Code Playgroud)

vgu*_*zzi 5

您的近似是正确的。它返回以米为单位的距离。

您可以将其除以1000,将其转换为KM,如下所示:

float distance = locationA.distanceTo(locationB)/1000;
Run Code Online (Sandbox Code Playgroud)

distanceTo 在此处了解更多信息。

  • 噢,谢谢您询问locationA参数。位置A参数是由一种方法提供的,在该方法中,我发现我实际上是在经度参数上传递了纬度值而不是我的经度值。 (3认同)