I have a string like 24.5 km10.1 km30.9 km
.
I want to compute the sum of all the distances in the string but I'm not able to do it. I'm getting a NumberFormatException
.
Here is my code
String stringValue = getDistanceOnRoad(lat, long, lat2, long2);
double sumValue = 0.0;
Log.d("my string is", stringValue);
try {
sumValue = Double.parseDouble(stringValue);
Log.d("distance there plus", String.valueOf(sumValue));
} catch (NumberFormatException e){
System.out.println("not a number");
}
I'm getting the distance from this method, everything is working fine but I'm not able to compute the sum of all distances.
How to calculate distance between two locations using their longitude and latitude value
private String getDistanceOnRoad(double latitude, double longitude,
double prelatitute, double prelongitude) {
String result_in_kms = "";
String url = "http://maps.google.com/maps/api/directions/xml?origin="
+ latitude + "," + longitude + "&destination=" + prelatitute
+ "," + prelongitude + "&sensor=false&units=metric";
String tag[] = { "text" };
HttpResponse response = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
response = httpClient.execute(httpPost, localContext);
InputStream is = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(is);
if (doc != null) {
NodeList nl;
ArrayList args = new ArrayList();
for (String s : tag) {
nl = doc.getElementsByTagName(s);
if (nl.getLength() > 0) {
Node node = nl.item(nl.getLength() - 1);
args.add(node.getTextContent());
} else {
args.add(" - ");
}
}
result_in_kms = String.format("%s", args.get(0));
}
} catch (Exception e) {
e.printStackTrace();
}
return result_in_kms;
}