如何显示 Location.distanceBetween 的结果

How to display result of Location.distanceBetween

编辑: 有人建议我使用 this 公式来计算我当前位置和放置的标记之间的距离,但我得到的是相同的无论我将标记放在哪里,结果都是 1.004822115417686E7。我将在更新的代码中展示该方法。

我只想要从我的位置到放置的标记的距离。我尝试使用 distanceTo(Location dest) 方法,但我不知道如何将 Marker LatLng 转换为 Location 对象(如果可能的话)。因此,我决定使用 Location.distanceBetween 方法。

问题是我的结果根本不正确。我究竟做错了什么?我是否误读了 Android 文档?

我所做的只是执行 onMapLongClick 添加标记。当前位置上没有标记。它将只是显示用户所在位置的蓝点。

地图使用基础:

用户停车。用户在他们停放的地图上放置标记。用户去附近的建筑物做某事。用户回来,打开应用程序,找到汽车。应用程序将显示到汽车的距离。距离不需要考虑障碍物、转弯等。直线距离即可。

代码:

 @Override
public void onMapLongClick(LatLng latLng) {
    mMap.clear();
    //m is the Marker
    m = mMap.addMarker(new MarkerOptions()
            .position(latLng)
            .title("My Ride")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker30x48)));
    float[] results = new float[1];
    Location currentPos = new Location(LocationManager.GPS_PROVIDER);
    latLng = m.getPosition();
    Location.distanceBetween(currentPos.getLatitude(), currentPos.getLongitude(), latLng.latitude, latLng.longitude, results);
    Log.d(TAG, "Distance using distanceBetween(): " + results[0] + " meters");
    Log.d(TAG, "Distance using meterDistanceBetweenPoints: " + meterDistanceBetweenPoints((float)currentPos.getLatitude(), (float)currentPos.getLongitude(), (float)latLng.latitude, (float)latLng.longitude));
    Toast.makeText(getApplicationContext(), "Marker Added", Toast.LENGTH_SHORT).show();
}
private double meterDistanceBetweenPoints(float lat_a, float lng_a, float lat_b, float lng_b) {
    float pk = (float) (180.f/Math.PI);

    float a1 = lat_a / pk;
    float a2 = lng_a / pk;
    float b1 = lat_b / pk;
    float b2 = lng_b / pk;

    double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2);
    double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2);
    double t3 = Math.sin(a1) * Math.sin(b1);
    double tt = Math.acos(t1 + t2 + t3);

    return 6366000 * tt;
}

这是 Logcat 中的输出:

Distance using distanceBetween(): 1.0061643E7 meters
Distance using meterDistanceBetweenPoints: 1.004822115417686E7

我在这里做错了什么?根据 Android Docs,它表示:

The computed distance is stored in results[0]

我实际上是在距离我的位置 20-30 英尺处放置了一个标记,那么这个奇怪的输出是什么?对于我尝试的新功能,它甚至都不正确。

onMapLongClick 中,您将通过以下行清除所有先前的标记:

mMap.clear();

因此每次放置新标记时,以前的标记都会被清除。如果你想在两个地方都有标记,即你的位置和点击的位置,那么每次在 onMapLongClick 上做:

 @Override
public void onMapLongClick(LatLng latLng) {
    mMap.clear();
    //marker at clicked location
    mMap.addMarker(new MarkerOptions()
            .position(latLng)
            .title("My Ride")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker30x48)));
    //marker at your location
    mMap.addMarker(new MarkerOptions()
                .position(MyLatLng)
                .title("My Location")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker30x48)));
    //----
    //do whatever else you want
    //----    
    }

MyLatLng你可以从onLocationChanged函数得到:

Location MyLatLng;
@Override
public void onLocationChanged(Location location) {
    MyLatLng = location;
}

计算两个位置之间的距离参考这个解决方案:

好的,所以我想我明白了,除非有人发布更好的答案。由于涉及代码,我将其发布为答案。我通过创建一个 LocationManager 对象添加了一些代码,并在验证我的 mLocationPermissionGranted 布尔值是否为真后将我的代码放在 try/catch 块中(这是因为我已经在这一步之前处理了它)。然后我把那个结果从米转换成英尺。这是更新后的函数:

@Override
public void onMapLongClick(LatLng latLng) {
    mMap.clear();
    //m is the Marker
    m = mMap.addMarker(new MarkerOptions()
            .position(latLng)
            .title("My Ride")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker30x48)));

    float[] results = new float[1];
    //New LocationManager object
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    try {
        if (mLocationPermissionsGranted) { //<-- already checked before, so it's true
            Location currentPos = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
            latLng = m.getPosition();
            Location.distanceBetween(currentPos.getLatitude(), currentPos.getLongitude(), latLng.latitude, latLng.longitude, results);
            Log.d(TAG, "Distance using distanceBetween(): " + (results[0])/0.3048 + " feet"); //<-- CONVERT FROM METERS TO FEET
            Log.d(TAG, "Distance using meterDistanceBetweenPoints: " + (meterDistanceBetweenPoints((float) currentPos.getLatitude(), (float) currentPos.getLongitude(), (float) latLng.latitude, (float) latLng.longitude))/0.3048); //<-- CONVERT FROM METERS TO FEET
            //Toast.makeText(getApplicationContext(), "Marker Added", Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "Result: " + (results[0])/0.3048 + " feet", Toast.LENGTH_LONG).show();
        }
    } catch(SecurityException e) {
        Log.d(TAG, "Exception: " + e.getMessage());
    }
}
private double meterDistanceBetweenPoints(float lat_a, float lng_a, float lat_b, float lng_b) {
    float pk = (float) (180.f/Math.PI);

    float a1 = lat_a / pk;
    float a2 = lng_a / pk;
    float b1 = lat_b / pk;
    float b2 = lng_b / pk;

    double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2);
    double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2);
    double t3 = Math.sin(a1) * Math.sin(b1);
    double tt = Math.acos(t1 + t2 + t3);

    return 6366000 * tt;
}

我测量了我站立的位置的长度,结果大约是 35 英尺。我使用的是卷尺,所以它不是很准确,但它就足够了。我在 Logcat 中的结果是:

Distance using distanceBetween(): 34.041595584138484 feet
Distance using meterDistanceBetweenPoints: 33.671114637701

我想我明白了!如果有人有更好的解决方案,请告诉我。我乐于接受建议。