-1

I'm trying to understand if there is any way of retrieving centralised information about a user's traveled distance in a day / week or between specific dates. I know that the OS (if allowed to) collects information about the user locations, so I guess there could be a way to retrieve how much he traveled within a period of time.

Note: I'm trying to get a total amount of miles / km traveled, not the locations where the user has been or at what times he traveled.

Note 2: I'm to know if it is possible to collect globally accumulated data by the OS over the overall geolocation services usage, without needing to have my app polling for the user location.

Thank you all!

tf.alves
  • 919
  • 8
  • 15
  • you can continuously store the user locations over time, and then calculate the distances from the locations of the desired time period – Vladyslav Matviienko Mar 06 '19 at 06:12
  • @VladyslavMatviienko Thank you for your feedback, but I need to understand if I can collect global data obtained from the overall usage of the geolocation services. I don't want to have my app running in the background collecting data. – tf.alves Mar 06 '19 at 06:15
  • the geolocation servicesdon't track the user location continuously, therefore they for sure don't know all the locations of the user – Vladyslav Matviienko Mar 06 '19 at 06:16
  • @VladyslavMatviienko I get that it won't be that accurate, but I have this feeling that the data collected would be more than enough. Taking iOS location services as an example, they collect the user location from time to time, going to the extent of pinpointing the places you've been to, how long you've been there, etc. I was curious to know if we can retrieve anything from this information. In my particular scenario, I don't need to be invasive with the user's location, I just need to have a rough estimate of the traveled distance. – tf.alves Mar 06 '19 at 06:20
  • Did you tried to check if Google Fit API can provide this info? – Vadim Eksler Mar 06 '19 at 08:07
  • @VadimEksler That is a nice lead. I'll look into it, though I'm concerned it will only provide me with data collected from walking / running activities. – tf.alves Mar 06 '19 at 08:11
  • @tf.alves more info in my answer. – Vadim Eksler Mar 06 '19 at 08:21

2 Answers2

1

I think this will help to you. Google Fit API : request with FitnessOptions like

    FitnessOptions fitnessOptions = FitnessOptions.builder()
            .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.TYPE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)
            .build();

you will need to request GoogleSignIn.requestPermissions

and after permission make request like

    DataReadRequest readRequest = new DataReadRequest.Builder()
            .read(DataType.TYPE_DISTANCE_DELTA)
            .bucketByTime(8, TimeUnit.DAYS)
            .enableServerQueries()
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

and call

Fitness.getHistoryClient(
                this,
                GoogleSignIn.getLastSignedInAccount(this))
                .readData(readRequest)
                .addOnSuccessListener(new OnSuccessListener<DataReadResponse>() { ...

Tell me if it will success, I don't have time to check this current options but in my case DataType.TYPE_HEART_RATE_BPM works well.

Vadim Eksler
  • 865
  • 9
  • 24
  • Thank you so much for your help @VadimEksler, I need to try this one out. I'm concerned that it won't provide me with travel data while driving. – tf.alves Mar 06 '19 at 08:26
  • 1
    Take a look here https://developers.google.com/android/reference/com/google/android/gms/fitness/data/DataType to know about what it will give to you – Vadim Eksler Mar 06 '19 at 08:27
  • this will be interesting for you too https://stackoverflow.com/questions/55019490/how-do-i-get-the-steps-from-google-fit-api/55020038#55020038 – Vadim Eksler Mar 06 '19 at 11:09
0

Yes, you can but accuracy might (or might not) be the problem depending on your needs. All you need to do is receive location updates with the help of an instance of the FusedLocationProviderClient class.

Google has a lot of information about it in the Receiving location updates documention. Now, the key point is, once you start receiving locations in the location callback...

locationCallback = new LocationCallback() {
    @Override
    public void onLocationResult(LocationResult locationResult) {
        if (locationResult == null) {
            return;
        }
        for (Location location : locationResult.getLocations()) {
            // Update UI with location data
            // ...
        }
    };
};

You can make use of the Location.distanceTo(Location otherLocation) method to calculate the distance between two locations...

currentLocation.distanceTo(previousLocation);

Or, you can use the Location.distanceBetween() method to calculate the distance between two geographical points (lat,lng).

I did mention accuracy at the begining because the Location distance calculation is based on the WGS87 ellipsoid which doesn't take elevations into account. You mgiht better off reading and parsing device sensors data such as Gyroscope and Accelerometer

Leo
  • 14,625
  • 2
  • 37
  • 55
  • Hello, thanks for your feedback, but notice that I am requesting it to be retrieved globally, from any app. I don't want to have a specific app running in the background collecting the user data, I want to know if it is possible to collect globally accumulated data by the OS over the overall geolocation services usage. – tf.alves Mar 06 '19 at 06:14
  • Thing is...Android doesn't collect that data at all. You'll have to either write your own OS or develop an application for that – Leo Mar 06 '19 at 06:26
  • I'm not so sure about this. Even if Android itself does not collect that data, Google APIs do. That's what I am trying to understand. If there is any useful point where I can collect this data from. – tf.alves Mar 06 '19 at 06:32
  • @tf.alves Android does NOT collect it, Google Location Service does collect it but it's not exposed, it's internal and data doesn't even stay in the device for long . You have to develop your OWN application. Not sure what else I can add to this for you to understand it :| – Leo Mar 06 '19 at 07:01
  • no need to be rude. I'm just exploring possibilities, not saying you are not correct. I am fully aware on how to collect user data directly from an app by polling for the location, or even using things provided by the native SDK (e.g. Visits Location Service on iOS), I'm just trying to explore any possibility of getting this information passively, from previously collected user data. P.s. when I mentioned that Android may be collecting some data, I was referring to integrations it has with Google Services that *may be* active on your phone at any time. – tf.alves Mar 06 '19 at 08:09
  • @tf.alves sorry I wasn't being rude. I was trying to stress the fact that it's not possible. Google keeps that data internally – Leo Mar 06 '19 at 08:42
  • Google likely never disclose these data, because it will be against Privacy policy. – xomena Mar 06 '19 at 19:03