Manaf
Manaf

Reputation: 33

Time spent at a specific location using GPS sensor

I'm trying to implement a function to monitor the time spent at a specific location.

I have implemented the " requestLocationUpdates " function to receive updates about the current user's location using Google Maps API.

 public Location get_location(){
    if( is_walking ){
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
            Toast.makeText(context,"Permission not granted",Toast.LENGTH_SHORT).show();
            return null;
        }
        lm  = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean is_gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (is_gps_enabled){
           lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,15000,5,this);
            Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            return l;
        }else {
        }
        return null;
    }else

    return null;

Do I just keep saving the time of the updates in array and compare them with each others? wouldn't that be a memory consuming?

Upvotes: 0

Views: 67

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93569

The simplest possible implementation is to save the initial location returned by the callback in a variable. Whenever you get a new one, check how far away it is. If the distance is more than X, he has moved. Compare the time on the two events. That's how long he stayed in one place. X should be at least 10m, because GPS isn't accurate enough to get less than that without a lot of false positives.

That will kind of give you an answer. The next level is filtering out false positives. Every once in a while you'll just get a really weird result that could be a good portion of a mile off. You need to filter those out. I would start with just doing something simple like waiting til you get 4 or 5 results outside your radius, then assume he moved. This may allow you to lower your radius as well, as you're more isolated from noise.

Of course you'll also want to filter out very short stays as well, as if you're walking you may get several readings showing you in the same place because you don't walk all that fast. Easiest way to do this is to require you to have N results in roughly the same place before you consider yourself there.

That will get you started. From there, you'll need to see where the data is wrong and come up with your own solutions.

Upvotes: 1

Related Questions