13
double distance;  

Location locationA = new Location("point A");  

locationA.setLatitude(latA);  
locationA.setLongitude(lngA);  

Location locationB = new Location("point B");  

locationB.setLatitude(latB);  
LocationB.setLongitude(lngB);  

distance = locationA.distanceTo(locationB);  

the above code is not working and i am getting 0.0 Km as distance? Also in the constructor of the location class, what does the string provider mean. In the above code i am using PointA and PointB as the providers.

why is the above code not working?

thank you in advance.

LOGCAT 05-09 17:48:56.144: INFO/current loc(1573): lat 0.0 lng 0.0 05-09 17:48:56.155: INFO/checklocation loc(1573): lat 54.4288665 lng 10.169366

user590849
  • 11,655
  • 27
  • 84
  • 125
  • 1
    Are you sure your values of latA/latB and lngA/lngB are different, and in the correct range? Can you provide examples of values you're using? Also, note `distanceTo`'s result is in metres, not kilometres. – Dave May 09 '11 at 12:48
  • yes. i am converting to meters. i have put up the logcat result. the values that i am getting are different for the different locations.... – user590849 May 09 '11 at 13:01
  • You don't need to convert to metres, the result is in metres already. Please post a full code sample, as I believe the issue here is not in the code you've posted. – Dave May 10 '11 at 12:27

6 Answers6

18

Just a quick snippet since I didn't see a complete and simple solution with GeoPoints above:

public float getDistanceInMiles(GeoPoint p1, GeoPoint p2) {
    double lat1 = ((double)p1.getLatitudeE6()) / 1e6;
    double lng1 = ((double)p1.getLongitudeE6()) / 1e6;
    double lat2 = ((double)p2.getLatitudeE6()) / 1e6;
    double lng2 = ((double)p2.getLongitudeE6()) / 1e6;
    float [] dist = new float[1];
    Location.distanceBetween(lat1, lng1, lat2, lng2, dist);
    return dist[0] * 0.000621371192f;
}

If you want meters, just return dist[0] directly.

Glen
  • 577
  • 1
  • 6
  • 11
14

Try Location.distanceBetween(..)

Update:

If you are getting lat/lon from GeoPoint then they are in microdegrees. You must multiply by 1e6.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • For the same input, this will give the same result. I believe there's an issue in the input being used, from code not shown in the original question. – Dave May 10 '11 at 12:35
  • @user590849 - are you perhaps getting lon/lat from GeoPoint? Those are in microdegrees - you should multiply by 1e6. – Peter Knego May 10 '11 at 12:38
9

As stated above, the Location Class is the way to go. Here is the code I have used :

Location locationA = new Location("point A");

locationA.setLatitude(pointA.getLatitudeE6() / 1E6);
locationA.setLongitude(pointA.getLongitudeE6() / 1E6);

Location locationB = new Location("point B");

locationB.setLatitude(pointB.getLatitudeE6() / 1E6);
locationB.setLongitude(pointB.getLongitudeE6() / 1E6);

double distance = locationA.distanceTo(locationB);

In this example, both pointA and pointB are instances of the GeoPoint class.

Todd Painton
  • 701
  • 7
  • 20
2

An alternative way to accomplish the above,

    public class Distance {

    public static double distance(double lat1, double lon1, double lat2, double lon2) {

        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 == "N") {
        //dist = dist * 0.8684;
        //}
        return (dist);
    }


    public static final double PI = 3.14159265;
    public static final double deg2radians = PI/180.0;


    public static double getDistance(double latitude1, double longitude1, double latitude2,double longitude2) {

        double lat1 = latitude1 * deg2radians;
        double lat2 = latitude2 * deg2radians;
        double lon1 = longitude1 * deg2radians;
        double lon2 = longitude2 * deg2radians;
        // Williams gives two formulae;
        // this is the more accurate for close distances.
        // In practice, the two differed only in the 8th or 9th place, for
        // separations as small as 1 degree.
        double radd = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2),
                2.0)
                + Math.cos(lat1)
                * Math.cos(lat2)
                * Math.pow(Math.sin((lon1 - lon2) / 2), 2.0)));

        return radd;
    }





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

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

}
Hades
  • 3,916
  • 3
  • 34
  • 74
2

This is not an answer, but note the signature for the Location constructor is Location(String provider).

i.e. the String you pass to the constructor should be one of LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER or LocationManager.PASSIVE_PROVIDER, not "point A" and "point B".

Dave
  • 6,064
  • 4
  • 31
  • 38
0
double CalculateDistance( double nLat1, double nLon1, double nLat2, double nLon2 )
{
    double nRadius = 6371; // Earth's radius in Kilometers
    // Get the difference between our two points
    // then convert the difference into radians

    double nDLat = ToRad(nLat2 - nLat1);
    double nDLon = ToRad(nLon2 - nLon1);

    // Here is the new line
    nLat1 =  ToRad(nLat1);
    nLat2 =  ToRad(nLat2);

    double nA = pow ( sin(nDLat/2), 2 ) + cos(nLat1) * cos(nLat2) * pow ( sin(nDLon/2), 2 );

    double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
    double nD = nRadius * nC;

    return nD; // Return our calculated distance
}

http://www.jaimerios.com/?p=39

HaloWebMaster
  • 905
  • 6
  • 16
  • Posting "distanceBetween(a,b)" code snippets like this is not an answer. The question here is why is `locationA.distanceTo(locationB)` (allegedly) giving a result of 0 metres? – Dave May 10 '11 at 12:26