Tabnine Logo
LocationManager.getLastKnownLocation
Code IndexAdd Tabnine to your IDE (free)

How to use
getLastKnownLocation
method
in
android.location.LocationManager

Finding current android device location

Refine searchRefine arrow

  • LocationManager.requestLocationUpdates
  • LocationManager.isProviderEnabled
  • Location.getLongitude
  • Location.getLatitude
  • LocationManager.removeUpdates
  • Context.getSystemService
origin: stackoverflow.com

 LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
origin: stackoverflow.com

Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
  mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
if (location != null) {
  Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude());
  mLocationManager.removeUpdates(this);
origin: stackoverflow.com

  lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
  lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
if(network_enabled)
  lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), 20000);
  timer1.cancel();
  locationResult.gotLocation(location);
  lm.removeUpdates(this);
  lm.removeUpdates(locationListenerNetwork);
  timer1.cancel();
  locationResult.gotLocation(location);
  lm.removeUpdates(this);
  lm.removeUpdates(locationListenerGps);
     gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
   if(network_enabled)
     net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
origin: stackoverflow.com

 private void _getLocation() {
  // Get the location manager
  LocationManager locationManager = (LocationManager) 
      getSystemService(LOCATION_SERVICE);
  Criteria criteria = new Criteria();
  String bestProvider = locationManager.getBestProvider(criteria, false);
  Location location = locationManager.getLastKnownLocation(bestProvider);
  LocationListener loc_listener = new LocationListener() {

    public void onLocationChanged(Location l) {}

    public void onProviderEnabled(String p) {}

    public void onProviderDisabled(String p) {}

    public void onStatusChanged(String p, int status, Bundle extras) {}
  };
  locationManager
      .requestLocationUpdates(bestProvider, 0, 0, loc_listener);
  location = locationManager.getLastKnownLocation(bestProvider);
  try {
    lat = location.getLatitude();
    lon = location.getLongitude();
  } catch (NullPointerException e) {
    lat = -1.0;
    lon = -1.0;
  }
}
origin: multidots/android-app-common-tasks

try {
  locationManager = (LocationManager) context
      .getSystemService(context.LOCATION_SERVICE);
      .isProviderEnabled(LocationManager.GPS_PROVIDER);
  System.out.println("gps band chhe" + isGPSEnabled);
      .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
              .getLastKnownLocation(LocationManager.GPS_PROVIDER);
origin: googlecodelabs/android-lifecycles

void addLocationListener() {
  // Note: Use the Fused Location Provider from Google Play Services instead.
  // https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi
  mLocationManager =
      (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
  mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListener);
  Log.d("BoundLocationMgr", "Listener added");
  // Force an update with the last location, if available.
  Location lastLocation = mLocationManager.getLastKnownLocation(
      LocationManager.GPS_PROVIDER);
  if (lastLocation != null) {
    mListener.onLocationChanged(lastLocation);
  }
}
origin: stackoverflow.com

this.isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
this.isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
        MIN_TIME_BW_UPDATES,
        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    if (locationManager != null)   {
      location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
      updateCoordinates();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
        MIN_TIME_BW_UPDATES,
        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
      location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
      updateCoordinates();
origin: stackoverflow.com

    .getSystemService(Context.LOCATION_SERVICE);
try {
  if (locationManager.isProviderEnabled(provider)) {
    location = locationManager.getLastKnownLocation(provider);
origin: stackoverflow.com

super.onCreate();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
sendBroadcastMessage(locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER));
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE,
    new LocationListener() {
      @Override
origin: openbmap/radiocells-scanner-android

  @Override
  public void run() {
    mLocationManager.removeUpdates(locationListenerGps);
    mLocationManager.removeUpdates(locationListenerNetwork);
    Location net_loc  = null, gps_loc  = null;
    if (mGpsEnabled)
      gps_loc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (mNetworkEnabled)
      net_loc = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    //if there are both values use the latest one
    if (gps_loc != null && net_loc != null){
      if (gps_loc.getTime() > net_loc.getTime())
        mLocationResult.gotLocation(gps_loc);
      else
        mLocationResult.gotLocation(net_loc);
      return;
    }
    if (gps_loc != null) {
      mLocationResult.gotLocation(gps_loc);
      return;
    }
    if (net_loc != null) {
      mLocationResult.gotLocation(net_loc);
      return;
    }
    mLocationResult.gotLocation(null);
  }
}
origin: stackoverflow.com

try {
  locationManager = (LocationManager) mContext
      .getSystemService(LOCATION_SERVICE);
      .isProviderEnabled(LocationManager.GPS_PROVIDER);
      .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
      if (locationManager != null) {
        location = locationManager
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
          latitude = location.getLatitude();
        locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            MIN_TIME_BW_UPDATES,
        if (locationManager != null) {
          location = locationManager
              .getLastKnownLocation(LocationManager.GPS_PROVIDER);
          if (location != null) {
            latitude = location.getLatitude();
  locationManager.removeUpdates(GPSTracker.this);
origin: stackoverflow.com

 LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f, locationListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

double latitude=0;
double longitude=0;
latitude = location.getLatitude();
longitude = location.getLongitude();
origin: multidots/android-app-common-tasks

try {
  locationManager = (LocationManager) context
      .getSystemService(context.LOCATION_SERVICE);
      .isProviderEnabled(LocationManager.GPS_PROVIDER);
  System.out.println("gps band chhe" + isGPSEnabled);
      .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
              .getLastKnownLocation(LocationManager.GPS_PROVIDER);
origin: googlecodelabs/android-lifecycles

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void addLocationListener() {
  // Note: Use the Fused Location Provider from Google Play Services instead.
  // https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi
  mLocationManager =
      (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
  mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListener);
  Log.d("BoundLocationMgr", "Listener added");
  // Force an update with the last location, if available.
  Location lastLocation = mLocationManager.getLastKnownLocation(
      LocationManager.GPS_PROVIDER);
  if (lastLocation != null) {
    mListener.onLocationChanged(lastLocation);
  }
}
origin: stackoverflow.com

  Location location = locationManager.getLastKnownLocation(provider);
protected void onResume() {
  super.onResume();
  locationManager.requestLocationUpdates(provider, 400, 1, this);
protected void onPause() {
  super.onPause();
  locationManager.removeUpdates(this);
origin: facebook/facebook-android-sdk

String bestProvider = locationManager.getBestProvider(criteria, false);
if (bestProvider != null && checkForLocationPermissionsAndRequest()) {
  location = locationManager.getLastKnownLocation(bestProvider);
  if (locationManager.isProviderEnabled(bestProvider) && locationListener == null) {
    locationListener = new LocationListener() {
      @Override
    locationManager.requestLocationUpdates(bestProvider, 1, LOCATION_CHANGE_THRESHOLD,
        locationListener, Looper.getMainLooper());
origin: stackoverflow.com

 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null)
{
  map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));

  CameraPosition cameraPosition = new CameraPosition.Builder()
    .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
    .zoom(17)                   // Sets the zoom
    .bearing(90)                // Sets the orientation of the camera to east
    .tilt(40)                   // Sets the tilt of the camera to 30 degrees
    .build();                   // Creates a CameraPosition from the builder
  map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));       
}
origin: stackoverflow.com

        .getSystemService(Context.LOCATION_SERVICE);
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
  net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
origin: fossasia/pslab-android

/**
 * @return the best location fetched
 */
@SuppressLint("MissingPermission")
public Location getDeviceLocation() {
  if (bestLocation == null) {
    if (psLabPermission.checkPermissions((Activity) context, PSLabPermission.MAP_PERMISSION)) {
      locationManager.requestLocationUpdates(provider,
          UPDATE_INTERVAL_IN_MILLISECONDS, MIN_DISTANCE_CHANGE_FOR_UPDATES,
          locationListener);
      if (locationReady) {
        return locationManager.getLastKnownLocation(provider);
      } else {
        return dummyLocation();
      }
    } else {
      return dummyLocation();
    }
  } else {
    return bestLocation;
  }
}
origin: stackoverflow.com

locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  locationManager.requestLocationUpdates(
    provider_info,
    MIN_TIME_BW_UPDATES,
    location = locationManager.getLastKnownLocation(provider_info);
    updateGPSCoordinates();
locationManager.removeUpdates(GPSTracker.this);
android.locationLocationManagergetLastKnownLocation

Popular methods of LocationManager

  • isProviderEnabled
  • requestLocationUpdates
  • removeUpdates
  • getBestProvider
  • getAllProviders
  • getProviders
  • addGpsStatusListener
  • removeGpsStatusListener
  • getGpsStatus
  • requestSingleUpdate
  • addTestProvider
  • setTestProviderEnabled
  • addTestProvider,
  • setTestProviderEnabled,
  • setTestProviderLocation,
  • removeTestProvider,
  • getProvider,
  • clearTestProviderEnabled,
  • setTestProviderStatus,
  • addNmeaListener,
  • removeNmeaListener

Popular in Java

  • Making http post requests using okhttp
  • setScale (BigDecimal)
  • scheduleAtFixedRate (ScheduledExecutorService)
  • setRequestProperty (URLConnection)
  • Table (com.google.common.collect)
    A collection that associates an ordered pair of keys, called a row key and a column key, with a sing
  • Socket (java.net)
    Provides a client-side TCP socket.
  • Arrays (java.util)
    This class contains various methods for manipulating arrays (such as sorting and searching). This cl
  • Iterator (java.util)
    An iterator over a sequence of objects, such as a collection.If a collection has been changed since
  • BlockingQueue (java.util.concurrent)
    A java.util.Queue that additionally supports operations that wait for the queue to become non-empty
  • JTable (javax.swing)
  • Top plugins for Android Studio
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now