get Last Known Locations - Android Map

Android examples for Map:Last Location

Description

get Last Known Locations

Demo Code


import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import java.util.HashMap;

public class Main{
    public static HashMap<String, LocationInfo> getLastKnownLocations(
            Context context) {//from   w w  w  .  j a  va2 s . c  om
        HashMap<String, LocationInfo> hashMap = new HashMap();
        if (hasFineLocationPermission(context)) {
            String str = "gps";
            hashMap.put("gps", a(context, "gps"));
        }
        if (hasCoarseLocationPermission(context)) {
            str = "network";
            hashMap.put("network", a(context, "network"));
        }
        return hashMap;
    }
    public static boolean hasFineLocationPermission(Context context) {
        return context
                .checkCallingOrSelfPermission("android.permission.ACCESS_FINE_LOCATION") == 0;
    }
    private static LocationInfo a(Context context, String str) {
        Location lastKnownLocation = ((LocationManager) context
                .getSystemService("location")).getLastKnownLocation(str);
        if (lastKnownLocation == null) {
            return null;
        }
        LocationInfo locationInfo = new LocationInfo();
        locationInfo.time = lastKnownLocation.getTime();
        locationInfo.provider = lastKnownLocation.getProvider();
        locationInfo.latitude = lastKnownLocation.getLatitude();
        locationInfo.longitude = lastKnownLocation.getLongitude();
        locationInfo.accuracy = lastKnownLocation.getAccuracy();
        return locationInfo;
    }
    public static boolean hasCoarseLocationPermission(Context context) {
        return context
                .checkCallingOrSelfPermission("android.permission.ACCESS_COARSE_LOCATION") == 0;
    }
}

Related Tutorials