androider
androider

Reputation: 992

Calculating distance travelled as the user moves

I need to calculate distance as the user moves in android. I have the starting point coordinates and can get the user's current location. How should I calculate distance. I don't think I can call GoogleDistance Api as my application would be a real time application and calling this API would be extremely overhead or should calling GoogleDirectionsAPI would work fine.

Also I would like to test the distance travelled. How should I do it? Is there some way of sampling the data or do I really need to go out in the real world and actually travel and then test my application?

Edited: I want the distance travelled by the user and not the straight line distance between the points

Upvotes: 8

Views: 11390

Answers (4)

Manpreet Singh Dhillon
Manpreet Singh Dhillon

Reputation: 903

I hope following will help: In my app i started collecting lat longs after every 3fts (I set 3ft just for testing). I collected lat longs as follows:

enter image description here

Now I started calculating distance from point 1 to 2 and then 2 to 3 and then 3 to 4 and so on. After summing up I got total distance from 1 to 5. I used following functions to calculate distance:

    public double GetDistanceFromLatLonInKm(double lat1, double lon1, double lat2, double lon2)
    {
        final int R = 6371;
        // Radius of the earth in km
        double dLat = deg2rad(lat2 - lat1);
        // deg2rad below
        double dLon = deg2rad(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double d = R * c;
        // Distance in km
        return d;
    }
    private double deg2rad(double deg)
    {
        return deg * (Math.PI / 180);
    }

This method is calculating exact distance for me.

Upvotes: 12

Manikanta
Manikanta

Reputation: 3421

For the first question . You have origin coordinates and the present user location coordinates so what you can do is google api v3 library provides a method that takes origin LatLng and destination LatLng and gives the distance . Add api v3 jar to your android build path. For the second one goto google maps and give the location details it is accurate..

Upvotes: 0

BeingMIAkashs
BeingMIAkashs

Reputation: 1385

You can calculate the distance between two point using the following function

double distanceBetweenTwoPoint(double srcLat, double srcLng, double desLat, double desLng) {
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(desLat - srcLat);
    double dLng = Math.toRadians(desLng - srcLng);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(srcLat))
            * Math.cos(Math.toRadians(desLat)) * Math.sin(dLng / 2)
            * Math.sin(dLng / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double dist = earthRadius * c;

    double meterConversion = 1609;

    return (int) (dist * meterConversion);
}

For the second Part: To use mock or sample data you can use "Fake GPS" app from the play store, which will generate mock location in your device.

Happy Coding

Upvotes: 1

Ashish Tamrakar
Ashish Tamrakar

Reputation: 821

There are basically three ways to check the traveled distance -

  1. By using GPS, check the coordinate between two points using distanceTo of Location class.

  2. Using Accelerometer , you can check the movement of device using Accelerometer.

  3. Using Step detector - this is recently updated by Android.

Hope this helps you to achieve your goal :)

Upvotes: 0

Related Questions