Question

I have one app which count the traveled km. but when I am driving with my car in the first 1 or 2 km. everything is fine and after that my app get slow.For example when I am traveled 2km. my app is 1.93 or when I am traveled 70km. my app is 48km.

And I have another little problem. When I started first time the app,I get big value (for example 5536km.) and when I restarted the program I get normal value 0.0km.This is just when i get started the first time GPS if I turned off GPS and after that turned on I will get again the same problem.

Sorry for my bad english.

Thanks in advance.

public class Gps extends Activity   {

 TextView display;



  double km;
  double currentLon=0 ;
  double currentLat=0 ;
  double lastLon = 0;
  double lastLat = 0;
  double distance;
  double distanceMeters;
  double distanceKm;
  double distanceKm1;


  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gps);



   display = (TextView) findViewById(R.id.info);       




                LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE); 
                lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);
                Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

                display.setText("GPS not found");


                if(loc==null){
                    display.setText("GPS not found");
                    }
                    else{
                        //Get the last latitude and longitude
                        lastLon=loc.getLongitude();
                        lastLat=loc.getLatitude();
                        }




   }



 LocationListener Loclist = new LocationListener(){




@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

     //start location manager
     LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE);


     //Get last location
      location = lm.getLastKnownLocation(lm.GPS_PROVIDER);




    //Request new location
     lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);





      //get the current lat and long
     currentLat = location.getLatitude();
     currentLon = location.getLongitude();



Location locationA = new Location("point A");
    locationA.setLatitude(lastLat);
    locationA.setLongitude(lastLon);


Location locationB = new Location("point B");
        locationB.setLatitude(currentLat);
        locationB.setLongitude(currentLon);


        distanceMeters = locationA.distanceTo(locationB);

        distanceKm = distanceMeters / 1000f;



        display.setText(String.format("traveled km.\n%.2f km.",distanceKm));


  }



@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}



@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub



}



@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub



}






 };



}
Was it helpful?

Solution

I can answer your second question:

When I started first time the app,I get big value (for example 5536km.)

This is the distance from (0,0) (aequator) to your current location.
It seems that lastLon and lastLat have the initial value of 0. So you must avoid that lastLon and lastLat is invalid. Explicitly check for not imnitialized.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top