0

I'm trying to develop a Simple Application which calculates the travel distance based on current Latitude and longitude.

The basic problem i'm having is that sometimes the new Latitude and Longitude are far away than 3 - 2000 km.

is there any thing which i'm doing wrong? please advice

Here is what i'm doing...

 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);
    }
Faizan Ahmed
  • 251
  • 1
  • 7

1 Answers1

0

Try distanceTo method.

Example:

@Override
public void onLocationChanged(Location location) {
    if(oldLocation!=null)}{
        float f = oldLocation.distanceTo(location);
    }
Sudarshan
  • 18,140
  • 7
  • 53
  • 61
  • tried it as well.. the problem is the values of latitude and longitude not the distance – Faizan Ahmed Mar 10 '17 at 05:40
  • @FaizanAhmed: Can you elaborate more about the problem, Are you unable to get correct values for latitude and longitude? – Sudarshan Mar 10 '17 at 05:44
  • you may say... let me tell you with a scenerio.. Assume there is a Location A which has x,y latitude and longitude in on location change the next location B i'm getting has latitude and logitude far away than of Location A.. while the fastest interval is just 5 seconds.. but this does not happen every time.. and when it happens it disturbs the distance calculation – Faizan Ahmed Mar 10 '17 at 05:51