Joetjah
Joetjah

Reputation: 6132

Calculating distance between multiple geopositions in Java

I was wondering how to correctly use the Location.distanceBetween method in Java to get the distance through multiple points.

Say you have a latitudeList and longitudeList, and you'd use a for=loop through them, how to use that method (effectively)? I seem to get the wrong distance value.

The code I'm using currently (it's messy but it seemed allright):

float result = 0;
float[] resultList = new float[1];
for (int k = 0; k < latitudeList.size() - 1; k++)
{
    Location.distanceBetween(latitudeList.get(k), longitudeList.get(k), latitudeList.get(k+1), longitudeList.get(k + 1), resultList);                        
    result = result + resultList[0];
    resultList = new float[1];
}   

What did I do wrong?

Thank you for your help.

Upvotes: 0

Views: 1577

Answers (1)

Rajesh Pantula
Rajesh Pantula

Reputation: 10241

why are you doing???

result = result+resultat[0] ???

while your distance is in resultList[0].

modify your code like this

float result = 0;
float[] resultList = new float[1];
for (int k = 0; k < latitudeList.size() - 1; k++)
{
    Location.distanceBetween(latitudeList.get(k), longitudeList.get(k),           latitudeList.get(k+1), longitudeList.get(k + 1), resultList);                        
    result = result + resultList[0];

}   

Upvotes: 1

Related Questions