0

我正在尝试开发一个简单的应用程序,它根据当前的纬度和经度计算旅行距离。

我遇到的基本问题是有时新的纬度和经度距离超过 3 - 2000 公里。

有什么我做错了吗?请指教

这就是我正在做的...

 protected void startLocationUpdates() {
    if (mGoogleApiClient != null) {
        if (mGoogleApiClient.isConnected()) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission
                    .ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat

                            .checkSelfPermission(this, Manifest.permission
                                    .ACCESS_COARSE_LOCATION) !=
                            PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[]
                // permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the
                // documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            LocationServices.FusedLocationApi.requestLocationUpdates(
                    mGoogleApiClient, mLocationRequest, this);
        }
    } else {
        buildGoogleApiClient();
    }
}





 @Override
public void onLocationChanged(Location location) {
    if(oldLocation!=null)}{
    double oldLatitude = oldLocation.getLatitude(); 
    double oldLongitude = oldLocation.getLongitude();
    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();

    float[] f = new float[1];
     Location.distanceBetween(oldLatitude, oldLongitude, currentLatitude 
                            currentLongitude, f);
    }
4

1 回答 1

0

尝试distanceTo方法。

例子:

@Override
public void onLocationChanged(Location location) {
    if(oldLocation!=null)}{
        float f = oldLocation.distanceTo(location);
    }
于 2017-03-10T05:22:24.640 回答