How to calculate distance between set of Latitudes and Longitudes in android?

calculate distance between two latitude longitude points in java
calculate driving distance between two points android
how to calculate distance between two latitude and longitude in google map
formula to calculate distance between two latitude and longitude
calculate distance between two coordinates latitude longitude
android distance between two latlng
get distance between two points android google maps
calculate bearing between two coordinates latitude/longitude

I'm trying to calculate distance between LatLng points. It's easy to calculate for two coordinates. I have to calculate distance between more than two LatLngs and calculate the cumulative distance from a set of LatLngs. I calculated distance between two points as per the following code.

    Location loc1=new Location("");

    Location loc2=new Location("");

    DecimalFormat format=new DecimalFormat("#.##");
    double distanceInMeters = loc1.distanceTo(loc2)/1000;
    tvDistance.setText(format.format(distanceInMeters) + " Km's");

Now I have for example sixteen LatLng points. First is starting place and last is stopping place. I have the LatLngs in a ArrayList. tried the following code. But it caused ArrayIndexOutOfBoundException. Do anybody know a method please share with me. Thanks.

    private void calculateDistance() {

    for (int i=0;i<coordList.size();i++){
        LatLng l1=coordList.get(i);
        double lat1=l1.latitude;
        double lng1=l1.longitude;
        Location location1=new Location("");

        LatLng l2=coordList.get(i+1);
        double lat2=l2.latitude;
        double lng2=l2.longitude;
        Location location2=new Location("");

        DecimalFormat format=new DecimalFormat("#.##");
        double distance=location1.distanceTo(location2)/1000;
        Toast.makeText(MainPage.this,format.format(distance) + " Km's",Toast.LENGTH_SHORT).show();

You can use SphericalUtil.computeLength method:


Calculate Distance Between Two GPS Coordinates Using Google , This is How You Can Calculate Distance Between Two GPS In this Android app article, we'll develop a demo app that will display the .to(new LatLng(​latLng.latitude, latLng.longitude)) . fromParts("package", activity. Latitude/Longitude Distance Calculator Enter latitude and longitude of two points, select the desired units: nautical miles (nm), statute miles (sm), or kilometers (km) and click Compute . Latitudes and longitudes may be entered in any of three different formats, decimal degrees (DD.DD), degrees and decimal minutes (DD:MM.MM) or degrees

The line LatLng l2=coordList.get(i+1); causes the exception. jon

Java Android Program to Calculate Distance between two points , of the Program to Calculate Distance between two points using Latitude & Longitude in Android. package com.example.changeiamge; import android.​app. Here are three abstractions which is considered for calculating the distance between two lat and longs: a) Flat surface. The shortest distance between two points in a plain is a straight line and we can use Pythagoras Theorem to calculate the distance between two points.

->Its easy to calculate the distance if you have sets of latitudes and longitudes

ArrayList<RideRoutes> ride_route_list = new ArrayList<>();
//suppose "ride_route_list" contains collections of Latitudes and longitudes

String distance_covered_str;
double total_meters = 0.0;

for(int i = 0; i < (ride_route_list.size() - 1); i++){
    double previous_latitude = Double.parseDouble(ride_route_list.get(i).getRout_latitude());
    double previous_longitude = Double.parseDouble(ride_route_list.get(i).getRout_longitude());

    double updated_latitude = Double.parseDouble(ride_route_list.get(i+1).getRout_latitude());
    double updated_longitude = Double.parseDouble(ride_route_list.get(i+1).getRout_longitude());

    Location start_latlng = new Location("location_previous");

    Location end_latlng = new Location("location_updated");

    total_meters += start_latlng.distanceTo(end_latlng);

double distance_covered_km =  total_meters / 1000;
distance_covered_str = String.format(Locale.getDefault(), "%.2f", distance_covered_km);

Note: ->here ride_route_list is an ArrayList which contains the list of Latitude and longitude

RideRoutes Class Structure:

public class RideRoutes {
private String rout_latitude;
private String rout_longitude;

public RideRoutes(String rout_latitude, String rout_longitude) {
    this.rout_latitude = rout_latitude;
    this.rout_longitude = rout_longitude;

public String getRout_latitude() {
    return rout_latitude;

public void setRout_latitude(String rout_latitude) {
    this.rout_latitude = rout_latitude;

public String getRout_longitude() {
    return rout_longitude;

public void setRout_longitude(String rout_longitude) {
    this.rout_longitude = rout_longitude;

Calculate Distance by Latitude and Longitude using Java , Learn more on how to calculate the distance between two points (given the latitude/longitude of those points) using ASP. How to calculate distances using longitude and latitude. One of the most common ways to calculate distances using latitude and longitude is the haversine formula, which is used to measure distances on a sphere. This method uses spherical triangles and measures the sides and angles of each to calculate the distance between points.

Calculate Distance by Latitude and Longitude using JavaScript , This routine calculates the distance between two points (given the ::: //::: latitude/​longitude of those points). It is being used to calculate ::: //::: the distance  Latitude and longitude are used to determine one's exact position on Earth relative to two reference lines: the equator circling the planet horizontally (east-west) and a vertical line called the prime meridian circling it vertically. The distance between latitudes is about 69.5 miles.

Program for distance between two points on earth, Given latitude and longitude in degrees find the distance between two points on the earth. Image Source : Wikipedia. Examples: Input : Latitude 1:  Latitude and Longitude in Excel: Calculate Distance, Convert Degrees, and Geocode Addresses. If you have a long list of geographic coordinates to work with, a Microsoft Excel spreadsheet is sure to be useful. There are three basic Excel tools that can work for you, no matter how you want to manipulate your geographic coordinates.

Calculate distance and bearing between two Latitude/Longitude , Just as the initial bearing may vary from the final bearing, the midpoint may not be located half-way between latitudes/longitudes; the midpoint between 35°N  A degree of longitude is widest at the equator with a distance of 69.172 miles (111.321 kilometers). The distance gradually shrinks to zero as they meet at the poles. At 40 degrees north or south, the distance between a degree of longitude is 53 miles (85 kilometers).

  • That is an interesting looking for loop.
  • this line creates exception for (int i=0;i<coordList.size();i++){ just change it to for (int i=0;i<(coordList.size()-1);i++){
  • This is the most efficient and accurate computation.
  • No it is not the most acurate, but well sufficient. It uses haversine formula. Vicencies formula works on ellipsoid and is more acurate, but needs much more compuational power.
  • Where are these two methods defined arcHav(havDistance(lat1, lat2, lng1 - lng2)). archHav and havDistance?