How to save previous gps coordinates - android

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

In an attempt to simplify embloo's answer, you can just save your current location to your previous location variable before updating to the current location in the onLocationChanged method. Like this:
#Override
public void onLocationChanged(Location location) {
mPreviousLocation = mCurrentLocation;
mCurrentLocation = location;
// update values or UI below here
}

Use this code inside your onLocationChanged method. It stores your previous location coordinates into prev variable:
private LatLng prev;
private int flag=0;
#Override
public void onLocationChanged(Location location) {
LatLng current = new LatLng(location.getLatitude(), location.getLongitude());
if(flag==0)
{
prev=current;
flag=1;
}
//Do whatever you want here
prev=current;
current = null;
}

Use SharedPreferences for simple key-value storage.
// saving
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putLong("current_latitude", latitude)
.putLong("current_longitude", longitude)
.apply();
// loading
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
long latitude = prefs.getLong("current_latitude", 0);
long longitude = prefs.getLong("current_longitude", 0);

Related

Cannot resolve symbol "locationListener" - Android

I am having trouble implementing a LocationListener.
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
TextView latitudeTV;
TextView longitudeTV;
TextView altitudeTV;
TextView locationNameTV;
double latitude;
double longitude;
double altitude;
String locationName;
LocationManager locationManager;
Location location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitudeTV = (TextView) findViewById(R.id.latitude);
longitudeTV = (TextView) findViewById(R.id.longitude);
altitudeTV = (TextView) findViewById(R.id.altitude);
locationNameTV = (TextView) findViewById(R.id.locationName);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
onLocationChanged(location);
LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
altitude = location.getAltitude();
locationName = "Placeholder";
longitudeTV.setText("Longitude: \n" + longitude + "°");
latitudeTV.setText("Latitude: \n" + latitude + "°");
altitudeTV.setText("Altitude: \n" + altitude + "m");
locationNameTV.setText("Location: \n" + locationName);
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
};
}
}
I'm not sure if it's a syntax issue or a scoping issue, but I keep getting "unable to resolve symbol" errors. It's acting like I haven't implemented the LocationListener methods. I have tried adding implements LocationListener to MainActivity and that shows an error saying I have not implemented the LocationListener methods. I'm not sure where my mistake is.
EDIT:
I've changed my code per your answers. When I try to implement //locationManager.requestLocationUpdates(Context.LOCATION_SERVICE, 5000, 0, this); I get the following when trying to launch the app: LocationTestApp keeps stopping. I've commented out that line, and the app works again.
public class MainActivity extends AppCompatActivity implements LocationListener {
TextView latitudeTV;
TextView longitudeTV;
TextView altitudeTV;
double latitude;
double longitude;
double altitude;
LocationManager locationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitudeTV = (TextView) findViewById(R.id.latitude);
longitudeTV = (TextView) findViewById(R.id.longitude);
altitudeTV = (TextView) findViewById(R.id.altitude);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
//locationManager.requestLocationUpdates(Context.LOCATION_SERVICE, 5000, 0, this);
Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
onLocationChanged(location);
}
#Override
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
altitude = location.getAltitude();
longitudeTV.setText("Longitude: \n" + longitude + "°");
latitudeTV.setText("Latitude: \n" + latitude + "°");
altitudeTV.setText("Altitude: \n" + altitude + "m");
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
}
A LocationListener class, in this case an Activity, should be defined as below:
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class MainActivity extends AppCompatActivity implements LocationListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, this); // Note: You pass this, so the overridden methods
}
#Override
public void onLocationChanged(Location location) {
// Do your stuff here
}
#Override
public void onProviderDisabled(String provider) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
And this is the solution in jle's comments.
But if you want to use the locationListener that you've declared in your onCreate, you should declare it before using it, in this way:
...onCreate(...) {
...
LocationListener locationListener = new LocationListener() {...} // Define this...
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener); // ...and then call this
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
onLocationChanged(location);
}

android accurate gps. calculate steps taken

the app i am creating need to use gps to keep track of how many steps the user steps has taken during excercise. if i cannot count the steps at least i wanted to calculated very accurate distance.
my code can display a map and user location as a blue dot. it did follow me but is quite inaccurate sometimes.i can get the gps to get the current location but it is not very accurate. i heard there is a new location API but i could not find an example code. i can't spot the difference between them..
the following has a lot of code that is unnecessary the reason is i decided to not include the map.
my map api v2 cannot display on emulator. blue-stack displayed a blank map and cannot enter location data. i need to use the same emulator as my team. and i know that map is not a requirement but gps calculate steps/accurate distance is a requirement.
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity implements LocationListener, Runnable, TextWatcher{
protected LocationManager locationManager;
private GoogleMap googleMap;
Button btnStartMove,btnPause,btnResume,btnStop;
//TextView Distance;
static double n=0;
Long s1,r1;
double lat1,lon1,lat2,lon2;
double dis=0.0;
MyCount counter;
Thread t1;
EditText userNumberInput;
boolean bool=false;
int count=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if(isGooglePlay())
{
//setContentView(R.layout.activity_main);
setUpMapIfNeeded();
}
btnStartMove=(Button)findViewById(R.id.Start);//start moving
btnPause=(Button)findViewById(R.id.Pause);//pause
btnResume=(Button)findViewById(R.id.Resume);//resume
btnStop=(Button)findViewById(R.id.Stop);
userNumberInput=(EditText)findViewById(R.id.UserInput);
userNumberInput.addTextChangedListener(this);
//Distance=(TextView)findViewById(R.id.Distance);
btnStartMove.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//start(v);
//start(v);
}
});
btnPause.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//start(v);
}
});
btnResume.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//start(v);
}
});
btnStop.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//start(v);
}
});
}
#Override
public void afterTextChanged(Editable editable) {
try
{
int no = Integer.parseInt(editable.toString());
if(no >= 100)
{
editable.replace(0, editable.length(), "99");
}
}
catch(NumberFormatException e){}
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
private void setUpMapIfNeeded() {
if(googleMap == null)
{
Toast.makeText(MainActivity.this, "Getting map",
Toast.LENGTH_LONG).show();
googleMap =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.displayMap)).getMap();
if(googleMap != null)
{
setUpMap();
}
}
}
private void setUpMap()
{
//Enable MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
//Get locationManager object from System Service LOCATION_SERVICE
//LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
//Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
if(provider == null)
{
onProviderDisabled(provider);
}
//set map type
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//Get current location
Location myLocation = locationManager.getLastKnownLocation(provider);
if(myLocation != null)
{
onLocationChanged(myLocation);
}
locationManager.requestLocationUpdates(provider, 0, 0, this);
}
private boolean isGooglePlay()
{
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status == ConnectionResult.SUCCESS)
{
Toast.makeText(MainActivity.this, "Google Play Services is available",
Toast.LENGTH_LONG).show();
return(true);
}
else
{
GooglePlayServicesUtil.getErrorDialog(status, this, 10).show();
}
return (false);
}
#Override
public void onLocationChanged(Location myLocation) {
//Get latitude of the current location
double latitude = myLocation.getLatitude();
//Get longitude of the current location
double longitude = myLocation.getLongitude();
//Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
//Show the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(MainActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(MainActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void start (View v){
switch(v.getId()){
case R.id.Start:
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
lat2=location.getLatitude();
lon2=location.getLongitude();
bool=true;
t1=new Thread(this);
t1.start();
counter= new MyCount(30000,1000);
counter.start();
}
else
{
Toast.makeText(MainActivity.this, "null location",
Toast.LENGTH_LONG).show();
}
break;
case R.id.Pause:
counter.cancel();
bool=false;
break;
case R.id.Resume:
counter= new MyCount(s1,1000);
counter.start();
bool=true;
break;
// case R.id.Distance:
// double time=n*30+r1;
// //Distance.setText("Distance:" + dis);
// Toast.makeText(MainActivity.this,"distance in metres:"+String.valueOf(dis)+"Velocity in m/sec :"+String.valueOf(dis/time)+"Time :"+String.valueOf(time),Toast.LENGTH_LONG).show();
// Toast.makeText(MainActivity.this, "Value of Count:" + String.valueOf(count),
// Toast.LENGTH_LONG).show();
// break;
case R.id.Stop:
counter.cancel();
counter= new MyCount(30000,1000);
bool = false;
}
}
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
counter= new MyCount(30000,1000);
counter.start();
n=n+1;
}
#Override
public void onTick(long millisUntilFinished) {
s1=millisUntilFinished;
r1=(30000-s1)/1000;
//e1.setText(String.valueOf(r1));
}
}
public double getDistance(double lat1, double lon1, double lat2, double lon2) {
double latA = Math.toRadians(lat1);
double lonA = Math.toRadians(lon1);
double latB = Math.toRadians(lat2);
double lonB = Math.toRadians(lon2);
double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) +
(Math.sin(latA) * Math.sin(latB));
double ang = Math.acos(cosAng);
double dist = ang *6371;
return dist;
}
public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
{
}
#Override
public void run()
{
count=6;
//LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
lat1=location.getLatitude();
lon1=location.getLongitude();
while(bool==true)
{
if(lat1!=lat2 || lon1!=lon2)
{
dis+=getDistance(lat2,lon2,lat1,lon1);
lat2=lat1;
lon2=lon1;
count =7;
//Distance.setText("Distance: " + dis + " Count: " + count);
count++;
}
}
}}
Once you have the distance, you can divide by the persons average step length, which you can let the user enter as paramter.
This will not work when making an exercise whithout moving (on a step trainer).
But for walking or running outdoors this will work.

Android geolocation printing latitude and longitude

I'm working on an Android app which gets your current location upon clicking a button, and then prints it if you click another button.
Now, I've got all of the code done, yet I'm stuck into how to retrieve the info of the longitude and latitude to print it, because I earn it on another function.
I'll post my code below to explain what I mean in a clearer manner:
package com.example.geolocation;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener, LocationListener {
private LocationManager locationManager;
public String provider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button)findViewById(R.id.start);
start.setOnClickListener(this);
Button stop = (Button)findViewById(R.id.stop);
stop.setOnClickListener(this);
Button show = (Button)findViewById(R.id.show);
show.setOnClickListener(this);
TextView location = (TextView)findViewById(R.id.location);
TextView providers = (TextView)findViewById(R.id.providers);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
//funcions boto start
case R.id.start:
locationManager.requestLocationUpdates(
provider,
10000, //temps em ms (10s)
500, //distancia (meters)
this);
//mostrar provider
TextView location = (TextView)findViewById(R.id.location);
location.setText("Provider: " + provider);
break;
//funcio boto stop
case R.id.stop:
locationManager.removeUpdates(this);
break;
//funcio boto show
case R.id.show:
Location locations = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(
provider,
10000, //temps em ms (10s)
1000, //distancia (meters)
this);
//show long & lat
TextView providers = (TextView)findViewById(R.id.providers);
providers.setText("Latitude & longitude: " + ""); //com traslladar valor loc aqui?
break;
}
}
#Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
int lat = (int) (loc.getLatitude());
int lng = (int) (loc.getLongitude());
loc.toString();
}
#Override
public void onProviderDisabled(String np) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String p) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
The segment where I get the desired info is this one:
#Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
int lat = (int) (loc.getLatitude());
int lng = (int) (loc.getLongitude());
loc.toString();
}
As you can see, I turn the information into a String.
Now, my problem is: how do I call this info in the main function to print it? In this segment:
//funcio boto show
case R.id.show:
Location locations = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(
provider,
10000, //temps em ms (10s)
1000, //distancia (meters)
this);
//show long & lat
TextView providers = (TextView)findViewById(R.id.providers);
providers.setText("Latitude & longitude: " + ""); //com traslladar valor loc aqui?
break;
what you have to do is the following :
1st - declare a String to store your lattitude and longitude :
private String lattitude , longitude;
2nd - in your get onLocationChanged do the following :
#Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
int lat = (int) (loc.getLatitude());
int lng = (int) (loc.getLongitude());
lattitude = "lattitude = "+ lat ;
longitude = "longitude = "+ lng;
}
3rd - use the lattitude and longitude to print them in any place you want .
Hope that helps .

Android To Make A Running Distance Calculation app

I am started learning android a few days ago and I wanted to design an app where I can calculate the distance, time and speed I have run by using gps.
So I have tried couple of examples and gone through some of the tutorials. I have written the following code.
But the is just giving me the gps locations but it is not calculating the distance and speed can any one tell me where is my problem and how to solve it.
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class test extends Activity implements Runnable {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 3;
protected LocationManager locationManager;
Location location;
static double n=0;
Long s1,r1;
double plat,plon,clat,clon,dis;
MyCount counter;
Thread t1;
EditText e1;
boolean bool=true;
Button b1,b2,b3,b4,b5;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
b1=(Button)findViewById(R.id.button1);//<--- current position
b2=(Button)findViewById(R.id.button2);//<---- start moving.. calculates distance on clicking this
b3=(Button)findViewById(R.id.button3);//<--- pause
b4=(Button)findViewById(R.id.button4);//<-- resume
b5=(Button)findViewById(R.id.button5);// <-- get distance
e1=(EditText)findViewById(R.id.editText1);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
clat=location.getLatitude();
clon=location.getLongitude();
Toast.makeText(test.this, message,
Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(test.this, "null location",
Toast.LENGTH_LONG).show();
}
}
public void start (View v){
switch(v.getId()){
case R.id.button2:
t1=new Thread();
t1.start();
counter= new MyCount(30000,1000);
counter.start();
break;
case R.id.button3:
counter.cancel();
bool=false;
break;
case R.id.button4:
counter= new MyCount(s1,1000);
counter.start();
bool=true;
break;
case R.id.button5:
double time=n*30+r1;
Toast.makeText(test.this,"distance in metres:"+String.valueOf(dis)+"Velocity in m/sec :"+String.valueOf(dis/time)+"Time :"+String.valueOf(time),Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(test.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(test.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(test.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(test.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
counter= new MyCount(30000,1000);
counter.start();
n=n+1;
}
#Override
public void onTick(long millisUntilFinished) {
s1=millisUntilFinished;
r1=(30000-s1)/1000;
e1.setText(String.valueOf(r1));
}
}
public double getDistance(double lat1, double lon1, double lat2, double lon2) {
double latA = Math.toRadians(lat1);
double lonA = Math.toRadians(lon1);
double latB = Math.toRadians(lat2);
double lonB = Math.toRadians(lon2);
double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) +
(Math.sin(latA) * Math.sin(latB));
double ang = Math.acos(cosAng);
double dist = ang *6371;
return dist;
}
#Override
public void run() {
while (bool){
clat=location.getLatitude();
clon=location.getLongitude();
if(clat!=plat || clon!=plon){
dis+=getDistance(plat,plon,clat,clon);
plat=clat;
plon=clon;
}
}
}
}
Notice that you are not using the distanceBetween or distanceTo methods and opted to calculate the distance your own way. Does your formula calculate great circle distances? It looks simpler than it ought to be. Suggest you check this out.

how to keep application running in background? keep collecting data?

UPDATED AT BOTTOM
I have written an application that logs the users position, current speed, average speed and top speed. I would like to know how to make the application do the following things:
prevent the screen from turning off while it is open on the screen
if the user opens another app or returns to the home screen, gets a call etc, the app should keep collecting data
(or would it be better to just write all data to a database everytime the location is updated? and maybe have a button to signify when to start and stop collecting data?)
here is the code that I have written. (feel free to use it if you want and if you have any recommendations on how I might improve it I am very open to constructive criticism :D )
package Hartford.gps;
import java.math.BigDecimal;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class GPSMain extends Activity implements LocationListener {
LocationManager locationManager;
LocationListener locationListener;
//text views to display latitude and longitude
TextView latituteField;
TextView longitudeField;
TextView currentSpeedField;
TextView kmphSpeedField;
TextView avgSpeedField;
TextView avgKmphField;
//objects to store positional information
protected double lat;
protected double lon;
//objects to store values for current and average speed
protected double currentSpeed;
protected double kmphSpeed;
protected double avgSpeed;
protected double avgKmph;
protected double totalSpeed;
protected double totalKmph;
//counter that is incremented every time a new position is received, used to calculate average speed
int counter = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
run();
}
#Override
public void onResume() {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
super.onResume();
}
#Override
public void onPause() {
locationManager.removeUpdates(this);
super.onPause();
}
private void run(){
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setSpeedRequired(true);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//Acquire a reference to the system Location Manager
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
locationListener = new LocationListener() {
public void onLocationChanged(Location newLocation) {
counter++;
//current speed fo the gps device
currentSpeed = round(newLocation.getSpeed(),3,BigDecimal.ROUND_HALF_UP);
kmphSpeed = round((currentSpeed*3.6),3,BigDecimal.ROUND_HALF_UP);
//all speeds added together
totalSpeed = totalSpeed + currentSpeed;
totalKmph = totalKmph + kmphSpeed;
//calculates average speed
avgSpeed = round(totalSpeed/counter,3,BigDecimal.ROUND_HALF_UP);
avgKmph = round(totalKmph/counter,3,BigDecimal.ROUND_HALF_UP);
//gets position
lat = round(((double) (newLocation.getLatitude())),3,BigDecimal.ROUND_HALF_UP);
lon = round(((double) (newLocation.getLongitude())),3,BigDecimal.ROUND_HALF_UP);
latituteField = (TextView) findViewById(R.id.lat);
longitudeField = (TextView) findViewById(R.id.lon);
currentSpeedField = (TextView) findViewById(R.id.speed);
kmphSpeedField = (TextView) findViewById(R.id.kmph);
avgSpeedField = (TextView) findViewById(R.id.avgspeed);
avgKmphField = (TextView) findViewById(R.id.avgkmph);
latituteField.setText("Current Latitude: "+String.valueOf(lat));
longitudeField.setText("Current Longitude: "+String.valueOf(lon));
currentSpeedField.setText("Current Speed (m/s): "+String.valueOf(currentSpeed));
kmphSpeedField.setText("Cuttent Speed (kmph): "+String.valueOf(kmphSpeed));
avgSpeedField.setText("Average Speed (m/s): "+String.valueOf(avgSpeed));
avgKmphField.setText("Average Speed (kmph): "+String.valueOf(avgKmph));
}
//not entirely sure what these do yet
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, locationListener);
}
//Method to round the doubles to a max of 3 decimal places
public static double round(double unrounded, int precision, int roundingMode)
{
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, roundingMode);
return rounded.doubleValue();
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
#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
}
}
BOTH PROBLEMS SOLVED THANKS TO ANSWERS FROM Marco Grassi AND Marcovena.
New Code:
package Hartford.gps;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.widget.TextView;
public class GPSMain extends Activity {
//text views to display latitude and longitude
static TextView latituteField;
static TextView longitudeField;
static TextView currentSpeedField;
static TextView kmphSpeedField;
static TextView avgSpeedField;
static TextView avgKmphField;
static TextView topSpeedField;
static TextView topKmphField;
//objects to store positional information
protected static double lat;
protected static double lon;
//objects to store values for current and average speed
protected static double currentSpeed;
protected static double kmphSpeed;
protected static double avgSpeed;
protected static double avgKmph;
protected static double totalSpeed;
protected static double totalKmph;
protected static double topSpeed=0;
protected static double topKmph=0;
//counter that is incremented every time a new position is received, used to calculate average speed
static int counter = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wL = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,"My Tag");
wL.acquire();
startService(new Intent(this, Calculations.class));
latituteField = (TextView) findViewById(R.id.lat);
longitudeField = (TextView) findViewById(R.id.lon);
currentSpeedField = (TextView) findViewById(R.id.speed);
kmphSpeedField = (TextView) findViewById(R.id.kmph);
avgSpeedField = (TextView) findViewById(R.id.avgspeed);
avgKmphField = (TextView) findViewById(R.id.avgkmph);
topSpeedField = (TextView) findViewById(R.id.topspeed);
topKmphField = (TextView) findViewById(R.id.topkmph);
}
static void run(){
latituteField.setText("Current Latitude: "+String.valueOf(lat));
longitudeField.setText("Current Longitude: "+String.valueOf(lon));
currentSpeedField.setText("Current Speed (m/s): "+String.valueOf(currentSpeed));
kmphSpeedField.setText("Cuttent Speed (kmph): "+String.valueOf(kmphSpeed));
avgSpeedField.setText("Average Speed (m/s): "+String.valueOf(avgSpeed));
avgKmphField.setText("Average Speed (kmph): "+String.valueOf(avgKmph));
topSpeedField.setText("Top Speed (m/s): "+String.valueOf(topSpeed));
topKmphField.setText("Top Speed (kmph): "+String.valueOf(topKmph));
}
}
and
package Hartford.gps;
import java.math.BigDecimal;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class Calculations extends Service implements LocationListener {
static LocationManager locationManager;
LocationListener locationListener;
private static final String TAG = "Calculations";
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
run();
}
private void run(){
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setSpeedRequired(true);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//Acquire a reference to the system Location Manager
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
locationListener = new LocationListener() {
public void onLocationChanged(Location newLocation) {
GPSMain.counter++;
//current speed for the GPS device
GPSMain.currentSpeed = round(newLocation.getSpeed(),3,BigDecimal.ROUND_HALF_UP);
GPSMain.kmphSpeed = round((GPSMain.currentSpeed*3.6),3,BigDecimal.ROUND_HALF_UP);
if (GPSMain.currentSpeed>GPSMain.topSpeed) {
GPSMain.topSpeed=GPSMain.currentSpeed;
}
if (GPSMain.kmphSpeed>GPSMain.topKmph) {
GPSMain.topKmph=GPSMain.kmphSpeed;
}
//all speeds added together
GPSMain.totalSpeed = GPSMain.totalSpeed + GPSMain.currentSpeed;
GPSMain.totalKmph = GPSMain.totalKmph + GPSMain.kmphSpeed;
//calculates average speed
GPSMain.avgSpeed = round(GPSMain.totalSpeed/GPSMain.counter,3,BigDecimal.ROUND_HALF_UP);
GPSMain.avgKmph = round(GPSMain.totalKmph/GPSMain.counter,3,BigDecimal.ROUND_HALF_UP);
//gets position
GPSMain.lat = round(((double) (newLocation.getLatitude())),3,BigDecimal.ROUND_HALF_UP);
GPSMain.lon = round(((double) (newLocation.getLongitude())),3,BigDecimal.ROUND_HALF_UP);
GPSMain.run();
}
//not entirely sure what these do yet
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, locationListener);
}
//Method to round the doubles to a max of 3 decimal places
public static double round(double unrounded, int precision, int roundingMode)
{
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, roundingMode);
return rounded.doubleValue();
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
UPDATE FOR shababhsiddique
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class Calculations extends Service{
static LocationManager locationManager;
static LocationListener locationListener;
private static long timerTime = 1;
private static float timerFloatValue = 1.0f;
private Context context;
private int counter = 0;
#Override
public IBinder onBind(Intent intent) {return null;}
#Override
public void onCreate() {
context = this;
update();
}
protected void update(){
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setSpeedRequired(true);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//Acquire a reference to the system Location Manager
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
locationListener = new LocationListener() {
public void onLocationChanged(Location newLocation) {
counter++;
if(GPSMain.GPSHasStarted==0){
GPSMain.previousLocation = newLocation;
//gets position
GPSMain.lat = round(((double) (GPSMain.previousLocation.getLatitude())),3,BigDecimal.ROUND_HALF_UP);
GPSMain.lon = round(((double) (GPSMain.previousLocation.getLongitude())),3,BigDecimal.ROUND_HALF_UP);
GPSMain.startingLocation = GPSMain.previousLocation;
GPSMain.routeLat.add(Double.toString(GPSMain.startingLocation.getLatitude()));
GPSMain.routeLon.add(Double.toString(GPSMain.startingLocation.getLongitude()));
GPSMain.startTime = System.currentTimeMillis();
GPSMain.GPSHasStarted++;
Toast.makeText(context, "GPS Connection Established", Toast.LENGTH_LONG).show();
startService(new Intent(context, AccelerometerReader.class));
Toast.makeText(context, "Accelerometer Calculating", Toast.LENGTH_LONG).show();
Toast.makeText(context, "Have A Safe Trip!", Toast.LENGTH_LONG).show();
}
//gets position
GPSMain.lat = round(((double) (newLocation.getLatitude())),3,BigDecimal.ROUND_HALF_UP);
GPSMain.lon = round(((double) (newLocation.getLongitude())),3,BigDecimal.ROUND_HALF_UP);
if (newLocation.distanceTo(GPSMain.previousLocation)>2.0f){
GPSMain.distanceBetweenPoints = GPSMain.distanceBetweenPoints + newLocation.distanceTo(GPSMain.previousLocation);
}
//current speed for the GPS device
GPSMain.mpsSpeed = newLocation.getSpeed();
if (GPSMain.mpsSpeed>GPSMain.topMps) {GPSMain.topMps=GPSMain.mpsSpeed;}
//store location in order to calculate distance during next iteration.
GPSMain.previousLocation = newLocation;
if (counter % 20 == 0){
GPSMain.routeLat.add(Double.toString(GPSMain.previousLocation.getLatitude()));
GPSMain.routeLon.add(Double.toString(GPSMain.previousLocation.getLongitude()));
}
}
//not entirely sure what these do yet
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 20, locationListener);
}
//Method to round the doubles to a max of 3 decimal places
public static double round(double unrounded, int precision, int roundingMode){
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, roundingMode);
return rounded.doubleValue();
}
//formats the time taken in milliseconds into hours minutes and seconds
public static String getTimeTaken(long end, long start){
#SuppressWarnings("unused")
String formattedTime = "", hourHour = "", hourMin = ":", minSec = ":";
long timeTaken = end-start, hour = 0, min = 0, sec = 0;
timerTime = timeTaken;
timeTaken = (end-start)/1000;
if (timeTaken>9 ){
hourHour = "0";
hourMin = ":0";
if (timeTaken>=60){
if (timeTaken>= 3200){
hour = timeTaken/3200;
timeTaken = timeTaken%3200;
if (hour>9){
hourHour = "";
}
}
min = timeTaken/60;
timeTaken = timeTaken%60;
if (min >9){
hourMin = ":";
}
}
sec = timeTaken;
if(sec%60<10){
minSec = ":0";
}
return formattedTime = (hourHour+hour+hourMin+min+minSec+sec);
}
sec = timeTaken;
minSec = ":0";
hourMin = ":0";
hourHour = "0";
return formattedTime = (hourHour+hour+hourMin+min+minSec+sec);
}
public static double averageSpeed(){
//calculates average speed
if (timerTime==0){timerTime=1;}
timerFloatValue = (float) timerTime;
timerFloatValue = timerFloatValue/1000;
return GPSMain.avgMps = GPSMain.distanceBetweenPoints/timerFloatValue;
}
//rounds the float values from the accelerometer
static String roundTwoDecimalFloat(float a){
float b = a/9.8f;
String formattedNum;
NumberFormat nf = new DecimalFormat();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
formattedNum = nf.format(b);
return formattedNum;
}
}
Question 1: You must acquire a WakeLock . There are multiple types of wakelock, depending if you want only the cpu on or also the screen.
Question 2: You should do your collecting data stuff inside a Service and separate the graphical interface from the collecting data. The Service will continue to collect the data until you stop it if you implement it correctly.

Categories

Resources