Question

I am trying to make a application,which is take latitude and longitude on current place of the user. And count momentary,what distance is travel the user in km.

The "currentLat" and "currentLon" is current latitude and longitude of the user.But I don't know what latitude and longitude to put for "endLat" and "ednLon".

Sorry for my bad english.

Thanks in advance.

///////////////////////////////////

I made it the app,but now have just one little problem. When I started first time the program,I get value 5536 and when I restarted the program I get normal value 0.0

Again sorry for my bad english. :)

And guys thanks for helping me,you are the best :)

  public class Gps extends Activity   {

 TextView display;


  double currentLon=0 ;
  double currentLat=0 ;
  double lastLon = 0;
  double lastLat = 0;
  double distance;




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




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

                if(loc==null){
                    display.setText("No GPS location found");
                    }
                    else{
                        //set Current latitude and longitude
                        currentLon=loc.getLongitude();
                        currentLat=loc.getLatitude();

                        }
                //Set the last latitude and longitude
                lastLat=currentLat;
                lastLon=currentLon ;


}



 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 loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

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

      //Get new location
      Location loc2 = lm.getLastKnownLocation(lm.GPS_PROVIDER);

      //get the current lat and long
     currentLat = loc.getLatitude();
     currentLon = loc.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);

        double distanceMeters = locationA.distanceTo(locationB);

        double distanceKm = distanceMeters / 1000f;

        display.setText(String.format("%.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 2

But I don't know what latitude and longitude to put for "endLat" and "ednLon".

endLat, endLon is a confusing name:

You should have lastLat, lastLon: the lat,lon received in the previous call of onGpsUpdate, save that into your e.g distanceCalculator.lastLat where distanceCalculator is an object where you store that lastLat, and lastLon

Then you should have curLat, curLon: current lat, lon delivered in onGPSUpdate

Then calculate the distance from (lastLat, lastLon) -> (curLat, curLon), and update your distanceCalculator.lastLat and lastLon.

OTHER TIPS

Since you have two locations, why don't you consider using distanceTo:

float distanceMeters = location1.distanceTo(location2);
float distanceKm = distanceMeters / 1000f;

Link

var R = 6371; // Radius of the earth in km
var dLat = (lat2-lat1).toRad();  // Javascript functions in radians
var dLon = (lon2-lon1).toRad(); 
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
        Math.sin(dLon/2) * Math.sin(dLon/2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c; // Distance in km.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top