Loshi
Loshi

Reputation: 221

Android distance are wrong

lat = 6.92961234
lng = 79.84796614

lat1 = 6.85620511
lng1 = 79.91251376 

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

locationA.setLatitude(lat);
locationA.setLongitude(lng);

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

locationB.setLatitude(lati);
locationB.setLongitude(lngi);

double distancei = locationA.distanceTo(locationB);

String cc = Double.toString(distancei);

float[] results = new float[1];
Location.distanceBetween(lati / 1E6, lngi / 1E6, lat / 1E6, lng / 1E6,results);
float s = results[0] * 0.000621371192f;
String a2 = Float.toString(s);

Two methods gives me Different answers as well as BOTH SEEMS TO BE WRONG.. since i looked at the google maps and the answers it shows about 14km(around 8mils). But the answers are showing as 6point smthing. and i dont know its on meters/km/miles.

please help me out... thank you

Upvotes: 2

Views: 930

Answers (1)

NickT
NickT

Reputation: 23873

If you had taken the trouble to produce some code which compiled and ran, read the javadocs and had run the following corrected code through the debugger:

private void test() {

    double lat = 6.92961234;
    double lng = 79.84796614;

    double lati = 6.85620511;
    double lngi = 79.91251376;

    Location locationA = new Location("point A");
    locationA.setLatitude(lat);
    locationA.setLongitude(lng);
    Location locationB = new Location("point B");
    locationB.setLatitude(lati);
    locationB.setLongitude(lngi);

    double distancei = locationA.distanceTo(locationB);
    float[] results = new float[3];
    Location.distanceBetween(lati, lngi, lat, lng,  results);
    // Distance in metres is in results[0]
}

You would see that both distancei and results[0] give you the same answer which is 10807 metres. This seems about right to me.

Upvotes: 1

Related Questions