I have the following code that takes around 1.5 minutes to find a location on two tablets that I've tested on, and it also takes around 30 seconds to load on a phone without GSM/CDMA enabled. Does anyone have any ideas why this takes so long to run without cell service? I have declared the class containing this code to be a LocationListener.
Here is my code:
This is in my OnCreate method:
locationNotificationBus = new EventBus();
locationManager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
Criteria locationCriteria = new Criteria();
locationCriteria.setAccuracy( Criteria.ACCURACY_COARSE );
String provider = locationManager.getBestProvider( locationCriteria, false );
locationManager.requestLocationUpdates(provider,0,0,this);
This is declared inside of the class itself:
#Override
public void onLocationChanged(Location location) {
Log.wtf( "LOCATOR: ", "Location Has Changed." );
Log.d("New Location", "Lat: " + location.getLatitude() + " Long: " + location.getLongitude());
locationManager.removeUpdates(this);
locationNotificationBus.post(location);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("LOCATOR: ", "Status Has Changed.");
}
#Override
public void onProviderEnabled(String provider) {
Log.d("LOCATOR: ", "Provider enabled.");
}
#Override
public void onProviderDisabled(String provider) {
Log.d("LOCATOR: ", "Provider disabled.");
}
It is my understanding that Coarse location can also use Wi-Fi to determine location. Is this not the case? If not, how do I go about using Wi-Fi?
Related
Issue:
onLocationChanged location sometimes doesn't update after standing still for a few seconds.
Description: After the listener starts to work, and I move the device, location gets updated. Then I stand still and don't move. After that, I keep moving. What it logs doesn't change and it keeps logging the same location.
Phone and Android version: Samsung S6, Android 6.0.1
The example log: (before standing still)
onLocationChanged: new location (60.98367243212177, 25.638220444285892)
onLocationChanged: new location (60.98367227235308, 25.638231264557437)
onLocationChanged: new location (60.98367068663074, 25.638241549966416)
(after standing still)
onLocationChanged: new location (60.983707386852174, 25.63813532475042)
onLocationChanged: new location (60.983707386852174, 25.63813532475042)
onLocationChanged: new location (60.983707386852174, 25.63813532475042)
onLocationChanged: new location (60.983707386852174, 25.63813532475042)
My function to start the listener:
public void startLocationListener(){
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
MyLocationListener locationListener = new MyLocationListener();
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,0,locationListener);
}
LocationListener class:
private final class MyLocationListener implements android.location.LocationListener{
#Override
public void onLocationChanged(Location location) {
Log.d(TAG, "onLocationChanged: new location (" + location.getLatitude() +", " + location.getLongitude()+")");
}
#Override
public void onProviderDisabled(String provider) {
Log.d(TAG, "onProviderDisabled: ");
}
#Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "onProviderEnabled: ");
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "onStatusChanged: " + provider + " status " + status+ "extra " + extras.toString());
}
}
This does not happen all the time, I think it might have something to do with the satellite or the phone. Does someone have any idea about this? Thanks in advance.
I have implemented an android application using GoogleApiClient which sends the Location updates after every 5m displacement and it is working fine.
Consider sometimes will not get Gps Signal.In such situation how can we get the update can anyone please help me how to handle this situation?
try this,
Location nwLocation = appLocationService
.getLocation(LocationManager.NETWORK_PROVIDER);
if (nwLocation != null) {
double latitude = nwLocation.getLatitude();
double longitude = nwLocation.getLongitude();
Toast.makeText(
getApplicationContext(),
"Mobile Location (NW): \nLatitude: " + latitude
+ "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
}
the complete tutorial can be found here
please have a look this link.
and i used this code to fetch lat long:
public void getCurruntLocation() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
and
private class mylocationlistener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
latLng = location.getLatitude() + "," + location.getLongitude();
Toast.makeText(RecentActivity.this,
location.getLatitude() + "" + location.getLongitude(),
Toast.LENGTH_LONG).show();
}
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
and simple call getCurruntLocation() where you want to fetch.
GoogleApiClient uses a LocationRequest class for setting number of parameters. One of them - .setPrioryty. If you are using LocationRequest.PRIORITY_HIGH_ACCURACY in some cases googleApiClient won't call onLocationChanged() without gps, due to lack of accuracy of GPRS coordinates.
My app find out current location using GPS. It is working fine in outdoor. But where GPS is not available or poor it is trying to get update again and again and it drains battery. So i want to stop update when GPS is poor or unavailable. You may suggest to use
lm.removeUpdates(locationlistenerforGPS);
It is working fine when GPS is available not in indoor. I like to stop update when GPS is poor or unavailable.
my code is
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationlistenerforGPS = new mylocationlistenerGPS();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationlistenerforGPS);
My locationlistenerGPS() function is
private class mylocationlistenerGPS implements LocationListener {
#Override
public void onLocationChanged(Location location) {
counterGPS++;
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + " ");
Log.d("LOCATION CHANGED", location.getLongitude() + " ");
Toast.makeText(LocationActivity.this,"latitude: "+
location.getLatitude() + "longitude: " + location.getLongitude()
+ " Provider:" + location.getProvider() + " Accuracy:" + location.getAccuracy(),
Toast.LENGTH_LONG).show();
}
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
Thank you very much for any kind of assistance.
Don't use minTime and minDistance set to zero in requestLocationUpdates(). See requestLocationUpdates() documentation.
Bundle extras in onStatusChanged() may include satellites - the number of satellites used to derive the fix, this way you can define poor signal or you can use NmeaListener if you need additional information from GPS.
I have a program that employs GPS to localize a person every 2 minutes. For that, the code is:
private boolean flagLocalizacion = false;
private LocationListener locationListener;
private Location ultimaLocalizacion;
private LocationManager locationManager;
#Override
public void onCreate(Bundle savedInstanceState) {
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
if (location!=null) {
if(ultimaLocalizacion == null && flagLocalizacion) {
ultimaLocalizacion = location;
Toast.makeText(getApplicationContext(), "Longitude: " + location.getLongitude() + "Latitude: " + location.getLatitude(), Toast.LENGTH_SHORT).show();
} else if ((ultimaLocalizacion.getLatitude() != location.getLatitude()
|| ultimaLocalizacion.getLongitude() != location.getLongitude()) && flagLocalizacion) {
ultimaLocalizacion = location;
Toast.makeText(getApplicationContext(), "Longitude: " + location.getLongitude() + "Latitude: " + location.getLatitude(), Toast.LENGTH_SHORT).show();
}
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {
System.out.println("gps");
}
public void onProviderDisabled(String provider) {
System.out.println("No gps");
}
}; Button botonComenzar = (Button) findViewById(R.id.bComenzar);
botonComenzar.setOnClickListener(new OnClickListener() {
#SuppressWarnings("static-access")
public void onClick(View v) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 12000, 30, locationListener);
Toast.makeText(getApplicationContext(), "Activada Localizacion", Toast.LENGTH_SHORT).show();
flagLocalizacion = true;
}
});
My problem is:
This applicaction works correctly in version 2.2. But the software failed when I installed it in a phone that has 2.3.5., the The fail is that localice continually, not every 2 minutes. Do you have thoughts on why that is happening?
Thanks you.
Continuous GPS refresh (i.e., with an interval between GPS updates of 1 sec), regardless of the requested minTime, is a known issue with many Android phones prior to JellyBean.
Full discussion of this issue with the Android team is here:
https://android-review.googlesource.com/#/c/34230/
A new CTSVerifier test has been added in Android 4.1 JellyBean that should prevent this from happening on JellyBean and higher.
For devices pre-Jelly Bean that are affected by this, your only option for a workaround is to manually unregister and re-register the LocationListener at whatever time interval you want GPS to refresh.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I get the current GPS location programmatically in Android?
Got a code below.. Will this get me my own location..?
public class MyLocationListener implements LocationListener {
public static double latitude;
public static double longitude;
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
latitude=loc.getLatitude();
longitude=loc.getLongitude();
}
public void onProviderDisabled(String provider)
{
//print "Currently GPS is Disabled";
}
public void onProviderEnabled(String provider)
{
//print "GPS got Enabled";
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
I know this has been asked several times.. Please help me with the code...
You may use this code to start listing for GPS updates and do something with the data you receive. It also prints messages in the log file for easy debugging. Please, note, this function does not return any results, it only starts listening to GPS. Results will be provided some time later in // do something here with the new location data part, when you may save them somewhere.
private void getGpsData() {
locationManager = (LocationManager) owner.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i(TAG, "location change:" + location.toString());
String longitude = "Londitude: " + location.getLongitude();
String latitude = "Latitude: " + location.getLatitude();
if( location.hasAccuracy() ) { // good enough?
// do something here with the new location data
....
//
Log.i(TAG, "GPS listener done");
locationManager.removeUpdates(this); // don't forget this to save battery
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i(TAG, provider + " status:" + status);
}
public void onProviderEnabled(String provider) {
Log.i(TAG, provider + " enabled");
}
public void onProviderDisabled(String provider) {
Log.i(TAG, provider + " disabled");
}
};
Log.i(TAG,"GPS listener started");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener );
}