Example usage for android.location LocationManager getLastKnownLocation

List of usage examples for android.location LocationManager getLastKnownLocation

Introduction

In this page you can find the example usage for android.location LocationManager getLastKnownLocation.

Prototype

@RequiresPermission(anyOf = { ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION })
public Location getLastKnownLocation(String provider) 

Source Link

Document

Returns a Location indicating the data from the last known location fix obtained from the given provider.

Usage

From source file:Main.java

/**
 * To be used if you just want a one-shot best last location, iterates over
 * all providers and returns the most accurate result.
 *///from w  w w.j  av a 2 s.  c  om
public static Location getBestLastGeolocation(Context context) {
    LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = manager.getAllProviders();

    Location bestLocation = null;
    for (String it : providers) {
        Location location = manager.getLastKnownLocation(it);
        if (location != null) {
            if (bestLocation == null || location.getAccuracy() < bestLocation.getAccuracy()) {
                bestLocation = location;
            }
        }
    }

    return bestLocation;
}

From source file:Main.java

public static Location getLocation(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    // Gets the last known location from the best service.
    String bestLocationProvider = locationManager.getBestProvider(new Criteria(), true /*enabled only*/);
    return locationManager.getLastKnownLocation(bestLocationProvider);
}

From source file:Main.java

public static double[] getLastKnownLocation(Activity a) {
    LocationManager locationManager = (LocationManager) a.getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    System.out.println("provider: " + provider);
    Location location = locationManager.getLastKnownLocation(provider);
    return new double[] { location.getLatitude(), location.getLongitude() };
}

From source file:Main.java

/**
 * Returns current (last known) location of the system. If the context is missing permissions
 * (ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION) or location tracking is disabled by user, this
 * will fail and return null./*w  w  w.j av  a 2 s  .co m*/
 * 
 * @param context
 *            Application context of caller
 * @return Current location or null if we could not retrieve current location
 */
public static Location getCurrentLocation(Context context) {
    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = lm.getProviders(true);

    // Find most accurate last known location
    Location location = null;
    for (int i = providers.size() - 1; i >= 0; i--) {
        location = lm.getLastKnownLocation(providers.get(i));
        if (location != null)
            break;
    }

    return location;
}

From source file:Main.java

/**
 * Method to register location updates with a desired location provider.  If the requested
 * provider is not available on the device, the app displays a Toast with a message referenced
 * by a resource id.//from  w  w w . ja  v  a  2  s  .  c  o  m
 *
 * @param provider Name of the requested provider.
 * @param errorResId Resource id for the string message to be displayed if the provider does
 *                   not exist on the device.
 * @return A previously returned {@link android.location.Location} from the requested provider,
 *         if exists.
 */
public static Location requestUpdatesFromProvider(LocationManager locationManager,
        LocationListener locationListener, final String provider, final int errorResId) {
    Location location = null;

    if (locationManager.isProviderEnabled(provider)) {
        locationManager.requestLocationUpdates(provider, RUN_ONCE, RUN_ONCE, locationListener);
        location = locationManager.getLastKnownLocation(provider);
    }

    return location;
}

From source file:com.binomed.showtime.android.util.localisation.LocationUtils.java

public static Location getLastLocation(Context context, ProviderEnum provider) {
    Location result = null;//from  w ww . j  ava 2s  .c  om
    switch (provider) {
    case GPS_PROVIDER:
    case GSM_PROVIDER: {
        if (isLocalisationEnabled(context, provider)) {
            LocationManager locationManager = getLocationManager(context);
            if (locationManager != null) {
                result = locationManager.getLastKnownLocation(provider.getAndroidProvider());
            }
        }
        break;
    }
    default:
        break;
    }
    return result;
}

From source file:com.appdynamics.demo.gasp.utils.LocationServices.java

/**
 * Set Location Services and get current location
 *//*from  w  w w . jav  a2s.c o  m*/
public static Location getLocation(Context context) {
    Location location = null;
    try {
        String svcName = Context.LOCATION_SERVICE;
        LocationManager locationManager = (LocationManager) context.getSystemService(svcName);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setCostAllowed(true);
        String provider = locationManager.getBestProvider(criteria, true);
        location = locationManager.getLastKnownLocation(provider);

        Log.i(TAG, "Current Latitude = " + location.getLatitude());
        Log.i(TAG, "Current Longitude = " + location.getLongitude());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return location;
}

From source file:com.airbop.library.simple.CommonUtilities.java

/**
 * Get the last location from the LocationManager, if it's available, if not
 * return null.//from w  w  w  . j  ava 2  s .c  om
 * @param appContext
 * @return
 */
public static Location getLastLocation(final Context appContext) {
    Location location = null;
    if (true) {
        Criteria criteria = getCriteria();
        LocationManager locationManager = (LocationManager) appContext
                .getSystemService(Context.LOCATION_SERVICE);
        if (locationManager != null) {
            String provider = locationManager.getBestProvider(criteria, true);
            if (provider != null) {
                location = locationManager.getLastKnownLocation(provider);

                if (location != null) {
                    displayMessage(appContext, String.format(AirBopStrings.airbop_got_last_location,
                            location.getLatitude(), location.getLongitude()));
                }
            }
        }
    }
    return location;
}

From source file:uk.ac.horizon.ug.exploding.client.LocationUtils.java

public static Location getCurrentLocation(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    for (int pi = 0; pi < PROVIDERS.length; pi++) {
        String provider = PROVIDERS[pi];
        if (!locationManager.isProviderEnabled(provider)) {
            Log.e(TAG, "Required location provider " + provider + " disabled (getCurrentLocation)");
        } else {/*from   w w  w .  ja  va2  s  .c  o  m*/
            Location loc = locationManager.getLastKnownLocation(provider);
            if (loc != null) {
                long age = System.currentTimeMillis() - loc.getTime();
                if (age > MAX_CURRENT_LOCATION_AGE_MS) {
                    Log.w(TAG, "Location provider " + provider + " last location is too old (" + age + " ms)");
                } else {
                    // TODO accuracy requirement?
                    return loc;
                }
            }
        }
    }
    return null;
}

From source file:com.mobilevangelist.glass.helloworld.GetTheWeatherActivity.java

public static Location getLastLocation(Context context) {
    Location result = null;//w  ww  .ja va  2  s .  c o  m
    LocationManager locationManager;
    Criteria locationCriteria;
    List<String> providers;

    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    locationCriteria = new Criteria();
    locationCriteria.setAccuracy(Criteria.NO_REQUIREMENT);
    providers = locationManager.getProviders(locationCriteria, true);

    // Note that providers = locatoinManager.getAllProviders(); is not used because the
    // list might contain disabled providers or providers that are not allowed to be called.

    //Note that getAccuracy can return 0, indicating that there is no known accuracy.

    for (String provider : providers) {
        Location location = locationManager.getLastKnownLocation(provider);
        if (result == null) {
            result = location;
        } else if (result.getAccuracy() == 0.0) {
            if (location.getAccuracy() != 0.0) {
                result = location;
                break;
            } else {
                if (result.getAccuracy() > location.getAccuracy()) {
                    result = location;
                }
            }
        }
    }

    return result;
}