1

I am using for-loop below. The code calculates correctly if it is not inside the loop, but I want to create a list with no. of distance and time.

KIEL = new LatLng(lat, log);                    
for(i=0;i<jarray.length;i++){
    KIEL = new LatLng(lat, log);
    doc = md.getDocument(fromPosition, KIEL,
        GMapV2Direction.MODE_DRIVING);
    duration = "" + md.getDurationValue(doc);
    distance = md.getDistanceText(doc);

    HashMap<String, String> map = new HashMap<String, String>();
    map.put("distance", distance);
    map.put("time", duration );

    // // adding HashList to ArrayList
    locationlist.add(map);                      
}

doc is an object of Document class which is calculating distance and time between two locations. But when I am using this method inside the loop, it will show no data. How to fix it?

appukrb
  • 1,507
  • 4
  • 24
  • 53
user3234928
  • 31
  • 1
  • 6

2 Answers2

3

use Location.distanceTo(Location) it will give you distance between two different Location's.

like distance = currentLocation.distanceTo(newLocation);

Anjali Tripathi
  • 1,477
  • 9
  • 28
0

download library from here and import into your project

https://github.com/googlemaps/android-maps-utils

follow this link for you distance

googlemaps.github.io/android-maps-utils/javadoc/

use like this and you will get perfect area and distance and all ...

SphericalUtil.computeDistanceBetween(LatLng from,LatLng to);

Nitin
  • 258
  • 2
  • 8