位置对象有另一个城市的LNG



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

我遇到的基本问题是,有时新的纬度和经度远远超过3-2000 km。

我做错了什么吗?请建议

这是我在做的...

 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);
    }

尝试distanceto方法。

示例:

@Override
public void onLocationChanged(Location location) {
    if(oldLocation!=null)}{
        float f = oldLocation.distanceTo(location);
    }

最新更新