Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long ago was the last known location recorded?

I am getting my last known location but not how long it has been since my location was last updated. How can I find out how long it has been since the location was last updated?

LocationManager locationManager 
                        = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria c = new Criteria();
    c.setAccuracy(Criteria.ACCURACY_FINE);
    c.setAccuracy(Criteria.ACCURACY_COARSE);
    c.setAltitudeRequired(false);
    c.setBearingRequired(false);
    c.setCostAllowed(true);
    c.setPowerRequirement(Criteria.POWER_HIGH);
String provider = locationManager.getBestProvider(c, true);
Location location = locationManager.getLastKnownLocation(provider);
like image 451
maddy Avatar asked Mar 09 '13 07:03

maddy


People also ask

How far back does location History go?

Google just announced it will automatically delete your location history by default. Google will start automatically deleting users' location history after 18 months.

When did I last visit a location?

1. Open the Google Maps app on your Android or iOS device. Tap your profile picture or letter in the top-right corner and choose Your Timeline. This will show you a list of all your visited places.

How long does location history stay on iPhone?

Automatically delete your Location History You can choose to automatically delete Location History that's older than 3 months, 18 months, or 36 months. Settings. Under "Location settings," tap Automatically delete Location History.

Can you see past locations on Find My iPhone?

Choose Location Services. Select System Services at the bottom. Within System Services, locate and tap on Significant Locations. Select History.


1 Answers

Best option for both pre and post API 17:

public int age_minutes(Location last) {
    return age_ms(last) / (60*1000);
}

public long age_ms(Location last) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
        return age_ms_api_17(last);
    return age_ms_api_pre_17(last);
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private long age_ms_api_17(Location last) {
    return (SystemClock.elapsedRealtimeNanos() - last
            .getElapsedRealtimeNanos()) / 1000000;
}

private long age_ms_api_pre_17(Location last) {
    return System.currentTimeMillis() - last.getTime();
}

The pre 17 is not very accurate, but should be sufficient to test if a location is very old.

This, I should think, would be OK:

if (age_minutes(lastLoc) < 5) {
   // fix is under 5 mins old, we'll use it

} else {
   // older than 5 mins, we'll ignore it and wait for new one

}

The usual use case for this logic is when the app has just started and we need to know whether we must wait for a new location or can use the latest for now while we wait for a new location.

like image 182
weston Avatar answered Sep 18 '22 15:09

weston