-1

I am creating an android app which shows distance and duration of two marker points in the Map. In the onCreate() I have written the following code:

In MapsActivity.java

   private List<LatLng> getDirectionPolylines(List<RouteObject> routes){
        List<LatLng> directionList = new ArrayList<LatLng>();
        for(RouteObject route : routes){
            List<LegsObject> legs = route.getLegs();
            for(LegsObject leg : legs){
                String routeDistance = leg.getDistance().getText();
                String routeDuration = leg.getDuration().getText();
                setRouteDistanceAndDuration(routeDistance, routeDuration);
                List<StepsObject> steps = leg.getSteps();
                for(StepsObject step : steps){
                    PolylineObject polyline = step.getPolyline();
                    String points = polyline.getPoints();
                    List<LatLng> singlePolyline = decodePoly(points);
                    for (LatLng direction : singlePolyline){
                        directionList.add(direction);
                    }
                }
            }
        }
        return directionList;
    }

I am not clear how to calculate distance & duration in 'getText' in the code above. I was not able to see some APIs like Distancebetween() which is using LtnLtg as references. Please suggest how to calculate the distance and duration values.

garima
  • 5,074
  • 10
  • 44
  • 77

2 Answers2

1

You should get the LatLngs of those markers (You can do that by using

marker.getPosition().getLatitude() 

and

marker.getPosition().getLongitude()

Find the distance between these two LatLngs. There are several ways of doing that. You could use

Location.distanceBetween(
    startLatitude,
    startLongitude,
    endLatitude,
    endLongitude,
    results);

or

Location loc1 = new Location("");
loc1.setLatitude(lat1);
loc1.setLongitude(lon1);
Location loc2 = new Location("");
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);
float distanceInMeters = loc1.distanceTo(loc2);
  • My question is how to get these latitudes from the markers.I understand that I can use these APIs. – garima Jan 24 '17 at 21:01
-1
var lat = marker.getPosition().lat();
var lng = marker.getPosition().lng();