title: How to calculate distance between set of Latitudes and Longitudes in android?
body: <p>I'm trying to calculate <code>distance</code> between <code>LatLng</code> points. It's easy to calculate for two coordinates. I have to calculate distance between more than two <code>LatLngs</code> and calculate the cumulative distance from a set of LatLngs. I calculated distance between two points as per the following code.</p>
  
  <pre><code>tvDistance=(TextView)findViewById(R.id.textView4);
      Location loc1=new Location("");
      loc1.setLatitude(11.2805);
      loc1.setLongitude(77.5989);
  
      Location loc2=new Location("");
      loc2.setLatitude(11.2801);
      loc2.setLongitude(77.5976);
  
      DecimalFormat format=new DecimalFormat("#.##");
      double distanceInMeters = loc1.distanceTo(loc2)/1000;
      tvDistance.setText(format.format(distanceInMeters) + " Km's");
  </code></pre>
  
  <p>Now I have for example sixteen <code>LatLng</code> points. First is starting place and last is stopping place. I have the <code>LatLngs</code> in a <code>ArrayList</code>.  tried the following code. But it caused <code>ArrayIndexOutOfBoundException</code>.
  Do anybody know a method please share with me. Thanks.</p>
  
  <pre><code>    private void calculateDistance() {
  
      for (int i=0;i&lt;coordList.size();i++){
          LatLng l1=coordList.get(i);
          double lat1=l1.latitude;
          double lng1=l1.longitude;
          Location location1=new Location("");
          location1.setLatitude(lat1);
          location1.setLongitude(lng1);
  
          LatLng l2=coordList.get(i+1);
          double lat2=l2.latitude;
          double lng2=l2.longitude;
          Location location2=new Location("");
          location2.setLatitude(lat2);
          location2.setLongitude(lng2);
  
          DecimalFormat format=new DecimalFormat("#.##");
          double distance=location1.distanceTo(location2)/1000;
          Toast.makeText(MainPage.this,format.format(distance) + " Km's",Toast.LENGTH_SHORT).show();
      }
  }
  </code></pre>