1

How do you get distance between multiple latitudes and longitudes from a encoded polyline.

Currenly the Direction API calculates the distance between origin and destination. But I also want to be able to calculate the distance whenthe origin destination is the same location.

 // Generates an encoded string with all the latitudes and longitues
 // used to get the exact distance, otherwise the google will calculate the closet path.
    String encodedLatLngList = PolyUtil.encode(latLngListTemp);
    String encodedWaypointString = "waypoints=enc:" + encodedLatLngList;

       RESTClient RESTClient = new RESTClient();
       Call<Direction> call = RESTClient.getGoogleApiService().getDirectionWithEncodedWaypoints(originLatLng, destinationLatLng, encodedWaypointString);
       call.enqueue(new Callback<Direction>() {

          @Override
          public void onResponse(Call<Direction> call, Response<Direction> response) {

          if (response.isSuccessful()) {

                 Direction direction = response.body();
             if (direction.getRoutes().size() > 0) {
                 String distance = direction.getRoutes().get(0).getLegs().get(0).getDistance().getText();
                 String locationA = direction.getRoutes().get(0).getLegs().get(0).getStartAddress();
                 String locationB = direction.getRoutes().get(0).getLegs().get(0).getEndAddress();
                 String duration = direction.getRoutes().get(0).getLegs().get(0).getDuration().getText();
Rasmus Rajje Josefsson
  • 1,564
  • 2
  • 15
  • 33

2 Answers2

1

Have you tried to use android.location.Location ? There's function distanceTo(Location) which calculates distance between two locations.

Meii
  • 33
  • 7
1

You can use SphericalUtil.computeLength from the Google Maps API Utility Library to compute the length of a List<LatLng>

antonio
  • 18,044
  • 4
  • 45
  • 61