Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find whether the device in same location or moving in Android? [closed]

Tags:

android

gps

In Android, I am getting locations for every 30 seconds by GPS using alarm manager. I want to detect whether the device is traveling or presence at same location using geopoints. If the device in same location GPS getting some times accurate data some times getting approximate data at the time 20 or 30 meter away from accurate location displaying on map.

So it's getting problem on finding device traveling.

How to find device is not traveling?

like image 871
Dhana Avatar asked Dec 15 '22 05:12

Dhana


2 Answers

If you have access to Play Services then take a look at Recognizing the User's Current Activity.

Else, in a locationListener you can do something like the following: As the GPS signal recieved can vary even if it's in the same position, you can't just compare the new location with the old one. I would use something like the following code

            double venueLat =latitude // Last known lat
            double venueLng = longitude // Last known lng

            double latDistance = Math.toRadians(userLat - venueLat);
            double lngDistance = Math.toRadians(userLng - venueLng);
            double a = (Math.sin(latDistance / 2) * Math.sin(latDistance / 2)) +
                            (Math.cos(Math.toRadians(userLat))) *
                            (Math.cos(Math.toRadians(venueLat))) *
                            (Math.sin(lngDistance / 2)) *
                            (Math.sin(lngDistance / 2));

            double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

            double dist = 6371 * c;             
            if (dist<0.01){ (in km, you can use 0.1 for metres etc.)
                /* If it's within 10m, we assume we're not moving */ 
            }
like image 151
perene Avatar answered May 12 '23 05:05

perene


you could use the new activity recognition feature presented for play-services , and check that the user is "still" . read here , here and here for more info , and/or check out this video or this one . i've never tested it, but it looks very promising.

alternatively, if you insist in using gps, you could define a range that the user is considered as staying at the same position, and check for each sample that the user doesn't go outside of this range.

it's important to set a range not too small and not too large since there is always a mistake-margin.

like image 27
android developer Avatar answered May 12 '23 05:05

android developer