I am trying to track my accurate walking speed. I am using the getSpeed() method of the LocationManager class. The problem is, the speed is not accurate, also answered by AlexWien here . Wein claims "The higher the speed, the more acurate it is. Under 10km/h it does not work well." So what would be the most precise way to do this if getSpeed is not built for precision? I did not stumble upon anything else yet.
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
location.getLatitude();
Toast.makeText(context, "Current speed:" + location.getSpeed(),
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
PS:: I understand that both Location and Fine Location permissions are required in this case.
Related
I want to get my current latitude and longitude using Mobile network provider, not GPS. AKA it should work without GPS with just your sim card. i have found many tutorials online claiming they find location with gps or network. however, they all seem to use GPS only! here or here and also here i don't want last knows location, as i don't wish to use GPS
Try This Code you can get current location without opening GPS
btnNWShowLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
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();
} else {
showSettingsAlert("NETWORK");
}
}
});
Do it via Location Manager.
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
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.NETWORK_PROVIDER, 0, 0, locationListener);
I am trying to get the location of the device and update to database continuously... However i just want to run that process in the background and update the location to the database in web server
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.makeUseOfNewLocation(location);
double latitude=location.getLatitude();
givenlatitude=String.valueOf(latitude);
double longitude=location.getLongitude();
Toast.makeText(getApplicationContext(),String.valueOf(latitude)+ " " +String.valueOf(longitude) , Toast.LENGTH_SHORT).show();
}
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.NETWORK_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, 1.0f, locationListener);
`
This is how i have been getting the location on interval time... i just want to run this code in the background and update it to the database peroidically!!!
the code below gives a NullPointerException is there any other way of getting current location in Android application
LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = location.getLatitude(); //NullPointerException
double longitude = location.getLongitude();
sure you could use the google play services location API:
http://developer.android.com/google/play-services/location.html
but maybe you should look into your problem first... see this question for your null problem:
android getlastknownlocation returns null
I am in a trouble about using LocationManager in Android.
The Locations from all LocationProvider's getLastKnownLocation are not enough in accuracy.
In my phone, Galaxy S3, 'network' and 'passive' give me the locations when I use the getLastKnownLocation method of the LocationProvider. 'gps' is disabled in my phone. The locations are..
network --> latitude : XX.5511981, longitude : XXX.1284714
passive --> latitude : XX.5511981, longitude : XXX.1284714
Both give me same location information. But the location is very wrong. It is about 50Km wrong location.
In that time, I run the Google maps app in the phone, which reports precise location point. How can the Google maps app obtain the higher precise location information than my app does?
PS. I registered LocationListners for every location provides with '0' minTime and '0' minDistance. But my phone never reports any location changes in several 10 minutes. I think this behavior is acceptable because the phone just stayed in my desk for that duration. But this condition is same for the Google maps app. It reports the phone's location exactly still.
UPDATE.
I tested my code in Galaxy S2. In this case, I requested 3 provides' getLastKnownLocation(GPS, NETWORK, PASSIVE). All the locations was null. And I registered LocationListeners for all the provides. It did not report anything. But in that time, the Google Map apps showed me precise location of the phone. Amazing!. Where does the Google Map apps get location information from?. Following is my test code.
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("location","location chagned x:"+location.getLatitude()+" y:"+location.getLongitude()+" prov:"+location.getProvider());
Date d = new Date(location.getTime());
Log.i("location","date:"+d);
}
public void onProviderDisabled(String provider)
{
Log.i("location","disable:" +provider);
}
public void onProviderEnabled(String provider)
{
Log.i("location","Enable:" +provider);
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.i("location","onStatusChanged:" +provider);
}
};
LocationListener locationListenerPassive = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("location","location chagned x:"+location.getLatitude()+" y:"+location.getLongitude()+" prov:"+location.getProvider());
Date d = new Date(location.getTime());
Log.i("location","date:"+d);
}
public void onProviderDisabled(String provider)
{
Log.i("location","disable:" +provider);
}
public void onProviderEnabled(String provider)
{
Log.i("location","Enable:" +provider);
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.i("location","onStatusChanged:" +provider);
}
};
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("location","location chagned x:"+location.getLatitude()+" y:"+location.getLongitude()+" prov:"+location.getProvider());
Date d = new Date(location.getTime());
Log.i("location","date:"+d);
}
public void onProviderDisabled(String provider)
{
Log.i("location","disable:" +provider);
}
public void onProviderEnabled(String provider)
{
Log.i("location","Enable:" +provider);
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.i("location","onStatusChanged:" +provider);
}
};
void showDebug(String provider)
{
Location location = lm.getLastKnownLocation(provider);
if(location!=null)
{
Log.i("location","all location provider:"+provider+" x:"+location.getLatitude()+" y:"+
location.getLongitude()+" actually provider:"+location.getProvider()+" accuracy:"+location.getAccuracy()+" time:"+location.getTime());
Date d = new Date(location.getTime());
Log.i("location","date:"+d);
if(this.current==null)this.current=location;
else if(this.current!=null && isBetterLocation(location,this.current))this.current=location;
}
else Log.i("location","all location provider:"+provider);
}
void start()
{
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
lm.removeUpdates(locationListenerNetwork);
lm.removeUpdates(locationListenerPassive);
lm.removeUpdates(locationListenerGps);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0, 0, locationListenerNetwork);
lm.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, locationListenerPassive);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
showDebug(LocationManager.NETWORK_PROVIDER);
showDebug(LocationManager.PASSIVE_PROVIDER);
showDebug(LocationManager.GPS_PROVIDER);
}
This is my code.
public class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
Location location = getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
LastLocationLat = location.getLatitude();
LastLocationLongi = location.getLongitude();
LocationLat = loc.getLatitude();
LocationLongi = loc.getLongitude();
if(loc.hasSpeed()) {
float mySpeed = loc.getSpeed();
Toast.makeText(getApplicationContext(), ""+mySpeed, 2000).show();
}
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(getApplicationContext(), "Gps onStatusChanged", Toast.LENGTH_SHORT).show();
}
}
But i did not get last location in this code i got same latitude and longitude
You are using LocationManager.NETWORK_PROVIDER to get locations. Your code implies you want GPS ("Gps Disabled")? In this case you should use LocationManager.GPS_PROVIDER.
LocationManager.NETWORK_PROVIDER is not very accurate (few hundred meters if locking to cell towers), so you might not detect the location change if you move less then 100m.
On the other hand, GPS is usually unavailable indoors or between tall buildings. Also GPS uses power so should not be used all the time (when app is in the background).
Read about Obtaining User Location, for the best approach to getting location with regards to accuracy vs service availability.
Hope this can help:
http://www.tutorials-android.com/learn/How_to_get_the_last_known_GPS_location.rhtml