Siva K
Siva K

Reputation: 4968

how to calculate the distance between two points in android app

in my app i am trying to calculate the distance a person traveling from one place to the other. For that i am using the Haversine formula,

R = earth’s radius (mean radius = 6,371km)
Δlat = lat2− lat1
Δlong = long2− long1
a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
c = 2.atan2(√a, √(1−a))
d = R.c 

getting the latitude and longitude of starting place and reaching place i am calculating the distance in kms. But others say that this distance calculation works only if travelled by airways and get varies if user travels by roadways.

If it is so how can i get a correct distance while traveling through road ways.

please help me friends

Upvotes: 3

Views: 15984

Answers (6)

Harald Wilhelm
Harald Wilhelm

Reputation: 6716

Location.distanceBetween() will do this for two Latitude Longitude coordinate points.

Upvotes: 3

Siva K
Siva K

Reputation: 4968

i found the sample code in the following link to calculate the distance going through roads.

http://code.google.com/p/krvarma-android-samples/source/browse/#svn/trunk/GPSSample%253Fstate%253Dclosed

Upvotes: 1

Maxim
Maxim

Reputation: 3006

This method is in standard API (Location.distanceBetween method)) http://developer.android.com/reference/android/location/Location.html

Upvotes: 6

Dinesh Sharma
Dinesh Sharma

Reputation: 11571

Use the following formula to find the distance between two lat-longs:

private double calculateDistance(double lat1, double lon1, double lat2, double lon2, String unit) 
{
      double theta = lon1 - lon2;
      double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
      dist = Math.acos(dist);
      dist = rad2deg(dist);
      dist = dist * 60 * 1.1515;
      if (unit == "K") {
        dist = dist * 1.609344;
      } else if (unit == "M") {
        dist = dist * 0.8684;
        }
      return (dist);
}

    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
    /*::  This function converts decimal degrees to radians             :*/
    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
    private double deg2rad(double deg) 
    {
      return (deg * Math.PI / 180.0);
    }

    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
    /*::  This function converts radians to decimal degrees             :*/
    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
    private double rad2deg(double rad)
    {
      return (rad * 180.0 / Math.PI);
    }

Pass lat,longs with the function and unit in which you want distance ("K" for kilometer and "M" for Miles). Hope this will help you.

Upvotes: 3

HighFlyer
HighFlyer

Reputation: 1655

Its seems distance trough radways bigger, because trains and trucks moves not along straight line but airplances do.

You need road map to occurate compute distance

Upvotes: 0

Aman Aalam
Aman Aalam

Reputation: 11251

When you travel by road (Even via air-travel too), bearing of the Earth comes into play since the Earth is not flat. but this Haversine formula should already take care of it. Secondly, what people might have been saying, is that, when you travel via road, you don't go just straight between two points. you take quite a number of turns. And to "precisely" tell the distance between two points, you might want to take those turns into consideration too.

Upvotes: 2

Related Questions