1

I wrote a program that calculates the distance between me and my place of work. The algorith is quite simple. I get the latitude and longitude of my position,and store it in a Location object. Then I create another Location object,where it store the lat and lon of my work's location. Then I am using the distanceTo(...) method Here is the program.

public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener,GoogleApiClient.ConnectionCallbacks,LocationListener{
private static final int PLAY_SERVICES_CODE = 90;
private static final String TAG = "Location";
Double latitude;
Double longitude;
Location destination;
Location currentLocation;
LocationRequest lq;
private TextView myLoc;
private GoogleApiClient googleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myLoc = (TextView)findViewById(R.id.myLocation);

    if(checkPlayServices()){
        initializeGoogleApiClient();
    }

}

private void initializeGoogleApiClient() {
    googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addOnConnectionFailedListener(this)
            .addConnectionCallbacks(this)
            .build();
}

private boolean checkPlayServices(){
    int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
    if(result!= ConnectionResult.SUCCESS){
        if(GooglePlayServicesUtil.isUserRecoverableError(result)){
            GooglePlayServicesUtil.getErrorDialog(result,this,PLAY_SERVICES_CODE).show();
        }
        return false;
    }
    return true;
}

@Override
public void onConnected(Bundle bundle) {
    Log.v(TAG,"onConnected");

    //Location loc = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    LocationRequest lq = LocationRequest.create();
    lq.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    lq.setFastestInterval(10000);
    //lq.setInterval(10000);

    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,lq,this);

}

@Override
public void onConnectionSuspended(int i) {
    Log.v(TAG,"connection has been suspended");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.v(TAG,"connection failed");

}

@Override
protected void onStart() {
    super.onStart();
    googleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();

    if(googleApiClient.isConnected()){
        googleApiClient.disconnect();
    }
}

@Override
public void onLocationChanged(Location loc) {
    Log.v(TAG,"connection has been suspended");
    //39,165397,22,399429
    if(loc!=null) {
       latitude = loc.getLatitude();
        longitude = loc.getLongitude();

        destination = new Location("");
        destination.setLatitude(39.639142);
        destination.setLongitude(22.334340);

        currentLocation = new Location("");
        currentLocation.setLatitude(latitude);
        currentLocation.setLongitude(longitude);

        float distance = currentLocation.distanceTo(destination);
        String stringDistance = String.valueOf(distance);
        String finalDistance = stringDistance.substring(3);

        myLoc.setText(finalDistance + " km");
    }else{
        myLoc.setText("No location obtained");
    }
}

@Override
protected void onResume() {
    super.onResume();
    if(googleApiClient.isConnected())
        LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,lq,this);
}
}

The value I am getting is 7.3 km. However,after a while the distance was decreased to 2.4 km and after few moments it was increased to 5.6 km!:):) Any ideas,why is this funny thing happening? The phone is on my table,and I don't move it at all.

Thanks

Theo
  • 3,099
  • 12
  • 53
  • 94
  • just check the current location returned. see if it changes over time. otherwise it may b problem with your algorithm. may be you are using some variable that's value need to be flushed but you are just adding value to it time and again – Zahan Safallwa Feb 20 '16 at 06:34
  • it comes closer to the correct distance,but it takes time... – Theo Feb 20 '16 at 06:37

0 Answers0