sarahsdev sarahsdev - 10 days ago 6
Android Question

method distanceTo(double) is undefined

I have a Problem to get the right way to get the distanceTo from my 2 Geo Points. How to get it work?

from the gps class:

double latitude; // latitude
String mlat;

latitude = location.getLatitude();
mlat = String.valueOf(latitude);


from the ListView:

// Get distance
String mReportA;
double mLocB;
int distance;
mReportA = e.getString("lat");
mReportA = e.getString("lon");
mLocB = gps.latitude;
mLocB = gps.longitude;
distance = mReportA.distanceTo(mLocB);


Error in Eclipse: The method
distanceTo(double)
is undefined for the type String
I get
e.getString("lat");
from json

Answer

To calculate distance between two geopoint you can follow the below example.

double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();

double endLatitude = lat;
double endLongitude = lng;

float[] results = new float[3];
Location.distanceBetween(currentLatitude, currentLongitude,endLatitude, endLongitude, results);

BigDecimal bd = new BigDecimal(results[0]);// results in meters
BigDecimal rounded = bd.setScale(2, RoundingMode.HALF_UP);
double values = rounded.doubleValue();

EDIT

if (values > 1000) {
 values = (Double) (values * 0.001f);// convert meters to Kilometers
 bd = new BigDecimal(values);
 rounded = bd.setScale(2, RoundingMode.HALF_UP);
 values = rounded.doubleValue();
}