Reputation: 1787
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
Upvotes: 1
Views: 1682
Reputation: 1589
I am not sure exactly what do you want anyway here are some pointers that might be useful for you
For Accurate data it is maybe better to stick with GPS provider only , 200 ms might be low you might want to make it 1+ second
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
Also there is no need to call getLastKnownLocation just start to listen and use these updated values here is a simplified implementation of your "MyLocationListener" class
public class MyLocationListener implements LocationListener{
private Location previousLocation;
@Override
public void onLocationChanged(Location location) {
if(previousLocation != null ){
//i already cached a previousLocation
//most recent (lat , lng) are stored in location
//you can access the previous (lat.lng) from previousLocation
//TODO:do your calculation and Send your notification here
}else{
//this is the first location i got i dont have anything to comapare it with
//nothing to do here i just added it for clarity
}
previousLocation = location;
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
//TODO:implement if you need it
}
@Override
public void onProviderEnabled(String s) {
//TODO:implement if you need it
}
@Override
public void onProviderDisabled(String s) {
//TODO:implement if you need it
}
}
Upvotes: 1
Reputation: 5797
I have an issue with the LocationManager's getLastKnownLocation function. It seems to always return NULL even if I have been using the My Location button in the map.
You can save the Location in a variable as suggested by @Gabe and you could store the location in the SharedPreferences so that when your application is launched, you will have an information of the last known location. Just saved the LatLng info everytime onLocationChanged is called.
Upvotes: 0
Reputation: 93542
You store it in a variable. Every time onLocationChanged is called, save the Location returned into mLastLocation. Then the next time onLocationChanged is called, mLastLocation will hold the old location and location will hold the new one.
Upvotes: 1