i have some code :
manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
activity fields
private LocationManager locationManager;
private LocationListener mlocListener;
onCreate initialization
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
class MyLocationListener
public class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location loc){
}
@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){
}
}
and i have button, onclick = getting users gps location
public void getUserLocation(View v){
boolean enabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Toast.makeText(getApplicationContext(), String.valueOf(location.getLatitude()) + " " + String.valueOf(location.getLongitude()), Toast.LENGTH_SHORT).show();
}
}
on getLastKnownLocation i have error. how can i check that locationManager has getLastKnownLocation ?? requestLocationUpdates automatic has to get location, hasnt it ??
And im interesting, which parametrs in requestLocationUpdates (minTime and minDistance) should better to use ?? i dont want eat users battery too much
Just a null check will do the required
As soon you request either from network or from Gps. It start looking for location updates.
but it can take times.
Gps updates are more slower to come. Need open sky but are more accurate.
network update are more faster to come. but are less accurate as compare to GPS.
Incase of wifi network accuracy comes around 50-100 which is good for most of the scnario.
use 0,0
as soon you get a update of desired accuracy unregister from listening location updates.