Reputation: 3901
What is the best way to track the latitude / longitude of the device in Android. I've tried many methods, non of which are working at all.
My device is running Android 4.1.2.
In my current code, sometimes GPS will work perfectly fine but sometimes it won't work (giving me latitude 0 and longitude 0).
Here is my current code:
locationManager = mLocationManager;
locationListener = new LocationListener() {
/**
* Fired when the location from the sensor changes.
*
* @param location Location object.
*/
@Override
public void onLocationChanged(Location location) {
sLatitude = String.valueOf(location.getLatitude());
sLongitude = String.valueOf(location.getLongitude());
sLatLon = sLatitude.concat(",").concat(sLongitude);
Log.d("TOMTOM LOCATION", location.toString());
}
/**
* Fired when the provider's status changes.
*
* @param provider The provider that has changed status.
* @param status The new status of the provider.
* @param extras Any extra data.
*/
public void onStatusChanged(String provider, int status, Bundle extras) {
// Do nothing.
}
/**
* Fired when the provider has been enabled.
*
* @param provider Provider that has been enabled.
*/
@Override
public void onProviderEnabled(String provider) {
// Do nothing.
}
/**
* Fired when the provider has been disabled.
*
* @param provider Provider that has been disabled.
*/
@Override
public void onProviderDisabled(String provider) {
// Do nothing.
}
};
Log.d("TOMTOM", locationManager.getProviders(true).toString());
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d("TOMTOM", "GPS enabled");
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
Log.d("TOMTOM", "Request GPS updates");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
Log.d("TOMTOM", "Request network updates");
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
} else {
Log.d("TOMTOM", "Fail");
}
}
} else {
Log.d("TOMTOM", "GPS disabled");
Log.d("TOMTOM", "Request network updates");
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
} else {
Log.d("TOMTOM", "Fail");
}
}
Any ideas why sometimes I get GPS updates (Logged as Request GPS updates
) and sometimes it will fail completely (Logged as Fail
)?
Upvotes: 0
Views: 324
Reputation: 485
It's been a year or so since I did GPS stuff on Android, so bear with me if I am not entirely right. Maybe someone can correct me.
The GPS system is not instant. You can not start your application and expect to have a location instantly. I believe the reason you get to the log telling you it's a Fail
simply is because Android does not always have a last known location. So you can get in situations where GPS is neither available (In buildings for example) where you also don't have a meaningful last known location. In this case it will simply be null.
Upvotes: 1