Thank you so much for your help, knowledge and time, it is priceless for me. I am working on a GPS location application. The basic idea is the behaviour of a compass. If a walk towards the latitude, so I am going to receive a notification about my direction is going to the north or south and the same with longitude .
Therefore, I have to save the past location known and the current location to compare between them and show the specific notification. Until now, I am able to recover the current location and the first location, but I can not recover the immediate previous location to compare with the current location. I will have to compare the latitude and longitude between this two points. Consequently, the previous location and actual location have to change according to the movement, but I can only get the updates for the actual location.
I share the source code that I have:
MainActivity.java:
package com.example.locationservicetest;
import android.app.Activity;
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.provider.Settings;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView latitude;
TextView longitude;
TextView prevLatitude;
TextView prevLongitude;
TextView provText;
double lat1;
double lon1;
LocationManager locationManager;
String provider;
MyLocationListener mylistener;
Criteria criteria;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitude = (TextView) findViewById(R.id.lat);
longitude = (TextView) findViewById(R.id.lon);
prevLatitude = (TextView) findViewById(R.id.prevLat);
prevLongitude = (TextView) findViewById(R.id.prevLon);
provText = (TextView) findViewById(R.id.prov);
//Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Define the criteria how to select the location provider
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); //default
//Charge of data?
criteria.setCostAllowed(false);
//Get the best provider depending on the criteria
provider = locationManager.getBestProvider(criteria, false);
//To receive the first location use cached location
Location location = locationManager.getLastKnownLocation(provider);
Location mLastLocation;
mylistener = new MyLocationListener();
if (location != null) {
mylistener.onLocationChanged(location);
mLastLocation=location;
lat1=mLastLocation.getLatitude();
lon1=mLastLocation.getLongitude();
prevLatitude.setText("Previous Latitude: "+String.valueOf(lat1));
prevLongitude.setText("Previous Longitude: "+String.valueOf(lon1));
} else {
//leads to the settings because there is no last known location
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
// location updates: at least 1 meter and 200millsecs change
locationManager.requestLocationUpdates(provider, 200, 1, mylistener);
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
// Initialize the location fields
latitude.setText("Latitude: "+String.valueOf(location.getLatitude()));
longitude.setText("Longitude: "+String.valueOf(location.getLongitude()));
Toast.makeText(getBaseContext(),"Location changed!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(getBaseContext(), provider + "'s status changed to "+status +"!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getBaseContext(), "Provider " + provider + " enabled!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getBaseContext(), "Provider " + provider + " disabled!",
Toast.LENGTH_SHORT).show();
}
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/prov"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No provider selected yet"
android:layout_marginTop="10dp"
android:textSize="20dp" />
<TextView
android:id="@+id/lat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Latitude: -"
android:textSize="20dp" />
<TextView
android:id="@+id/lon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Longitude: -"
android:textSize="20dp" />
<TextView
android:id="@+id/prevLat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="PrevLatitude: -"
android:textSize="20dp" />
<TextView
android:id="@+id/prevLon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="PrevLongitude: -"
android:textSize="20dp" />
</LinearLayout>
Thank you in advance for your kindness