Skip to content

Instantly share code, notes, and snippets.

@enginebai
Last active August 29, 2015 14:00
Show Gist options
  • Save enginebai/11180756 to your computer and use it in GitHub Desktop.
Save enginebai/11180756 to your computer and use it in GitHub Desktop.
Android getLocation()
public Location getCurrentLocation()
{
this.locationManager = (LocationManager) this.getSystemService( Context.LOCATION_SERVICE );
Location location = null;
boolean isGPSEnabled = locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER );
boolean isNetworkEnabled = this.locationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER );
if ( !( isGPSEnabled || isNetworkEnabled ) )
this.checkGPSSetting();
else
{
if ( location == null )
{
if ( isGPSEnabled )
{
this.locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this );
Log.d( LOG_TAG, "GPS enabled" );
location = this.locationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER );
}
if ( isNetworkEnabled )
{
this.locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this );
Log.d( LOG_TAG, "Network enabled" );
location = this.locationManager.getLastKnownLocation( LocationManager.NETWORK_PROVIDER );
}
}
}
return location;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment