Gets the most recent last known location which is the best one to use since the weather doesn't rely on a specific location. - Android Map

Android examples for Map:Last Location

Description

Gets the most recent last known location which is the best one to use since the weather doesn't rely on a specific location.

Demo Code


//package com.java2s;
import java.util.List;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;

public class Main {
    /**/*w  ww  . j  a  va2 s  .  c o  m*/
     * Gets the most recent last known location which is the best one to use
     * since the weather doesn't rely on a specific location.
     * 
     * @param context
     * @param locationManager
     * @return
     */
    public static Location getBestLastKnownLocation(Context context) {
        LocationManager locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);

        List<String> providers = locationManager.getAllProviders();
        Location location = null, temLocation;
        for (String provider : providers) {
            temLocation = locationManager.getLastKnownLocation(provider);

            if (location == null
                    || (temLocation != null && location.getTime() < temLocation
                            .getTime()))
                location = temLocation;
        }

        return location;
    }
}

Related Tutorials