I am making a program in android where i get the gps coordinates when the location changes. I need to save the previous latitude and longitude whenever the location changes. Then i will be able to calculate the distance later. Can anyone help me how to save the current coordinates and the previous coordinates programmatically. Thanks. Here is my code. Ignore the buttons and timer i have to store the previous coordinates when the location changes.
package com.example.higher.myosmand;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends Activity {
private LocationManager locationManager;
private LocationListener locationListener;
private Location currentLocation;
private TextView latlon;
private TextView distance;
ArrayList<String> list = new ArrayList<>();
ListView listView;
private TextView listText;
private float results[] = new float[2];
double lat;
double lon;
double currentLat;
double currentLon;
double accuracy;
double speed;
double altitude;
double time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latlon = (TextView) findViewById(R.id.latlon);
distance = (TextView) findViewById(R.id.distanceText);
listView = (ListView)findViewById(R.id.listView);
listText = (TextView)findViewById(R.id.listText);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location loc) {
//locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
lat = loc.getLatitude();
lon = loc.getLongitude();
accuracy = loc.getAccuracy();
speed = loc.getSpeed();
altitude = loc.getAltitude();
time = loc.getTime();
/*currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
currentLat = currentLocation.getLatitude();
currentLon = currentLocation.getLongitude();*/
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.activity_simplelist, R.id.listText, list));
list.add("latitude: " + lat + " longitude: " + lon);
//list.add("speed: " + speed + " accuracy: " + accuracy);
distance.setText("size: " + list.size());
}
@Override
public void onProviderDisabled(String arg0) {
//TODO auto generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
//TODO auto generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
//TODO auto generated method stub
}
};
}
public void setMyTimer(){
Timer myTimer = new Timer();
TimerTask myTask = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.activity_simplelist, R.id.listText, list));
list.add("latitude: " + lat + " longitude: " + lon);
//distance.setText("Current Location:\n Latitude: " + currentLat +" Longitude: " + currentLon);
}
});
}
};
myTimer.schedule(myTask, 5000, 5000);
}
public void startButton(View view) {
//locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
latlon.setText("latitude: " + lat + " \nlongitude: " + lon + " \naccuracy: " + accuracy +
"\nspeed: " + speed + "\naltitude: " + altitude + "\ntime: " + time);
}
public void endButton(View view) {
//locationManager.removeUpdates(locationListener);
//locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
public void resetButton(View view) {
list.clear();
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
@Override
protected void onPause() {
super.onPause();
//locationManager.removeUpdates(locationListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}