Praveen Kumar
Praveen Kumar

Reputation: 567

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

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.

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");

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("");
        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();
    }
}

Upvotes: 0

Views: 1086

Answers (3)

Hantash Nadeem
Hantash Nadeem

Reputation: 488

->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");
    start_latlng.setLatitude(previous_latitude);
    start_latlng.setLongitude(previous_longitude);

    Location end_latlng = new Location("location_updated");
    end_latlng.setLatitude(updated_latitude);
    end_latlng.setLongitude(updated_longitude);

    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;
}
}

Upvotes: 0

Jon Pedersen
Jon Pedersen

Reputation: 31

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

Upvotes: 3

Related Questions