4

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?

1
  • location.getSpeed doesn't do what you want ? or did you not read the documentation ?
    – njzk2
    Commented Sep 25, 2013 at 9:29

4 Answers 4

9

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 */ 
            }
1
  • Thank you for provide useful information about Play Services
    – Dhana
    Commented Sep 25, 2013 at 10:20
2

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.

4
  • Thank You. I got useful Information from you sent links.
    – Dhana
    Commented Sep 25, 2013 at 9:44
  • do note that the activity recognition feature will only work on android devices that have the play store installed (which is the case for the majority of them) Commented Sep 25, 2013 at 10:02
  • Is recognition feature will send device locations to Google Play Server?
    – Dhana
    Commented Sep 26, 2013 at 5:40
  • @Renga i don't think so. you should ask google about it. if they do send, i suppose it's anonymously , like the rest of their services. according to my logic, if it doesn't provide them any useful information, they don't need it. i think android already asks about sending data to google on the first time the device runs, and even then, it's anonymously , so it would be weird if this service is any different. one of the reasons to send location is in order to find the location better in the future, since you can cross the data with the wifi networks and cell towers around. Commented Sep 26, 2013 at 7:56
1

You can write code in onLocationChanged() method. There you will get new latitude longitude each and everytime when device changed its position. OnLocationChange() method called only if location has been changed. But if you want some specific distance wise calculations then you have to calculate distance between older lat-long and newer one. You can find distance using lat-long.

2
  • I didn't used onLocationChanged() method. I have used Pending Intent to get locations.
    – Dhana
    Commented Sep 25, 2013 at 9:34
  • Use LocationManager class it will give you accurate result as well as whatever calculations you need is you would get. Commented Sep 25, 2013 at 9:41
0

Every 30 seconds when you get the new locations you can compare it with the previous location to see if there is any difference in location and determine if the device is travelling.

1
  • I did like you said but GPS some times getting approximate location away from correct location at the time displaying as traveling.
    – Dhana
    Commented Sep 25, 2013 at 9:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.