Vemuri Pavan
Vemuri Pavan

Reputation: 505

How to calculate on road distance between two points?

In my android app I am planning to add delivery taxes for the products(X bucks per kilometer) so, how can i calculate the on road distance between two points ?

Our products will be dispatched from a fixed location, but the user will give the destination location.

Can anyone give a detailed approach for this?

Upvotes: 1

Views: 3378

Answers (3)

Sayem
Sayem

Reputation: 86

if you are using Google maps API and use polyLines, just call .getDistanceValue() to get the distance. the code below will show you how to show distance value in a textview calculating 2 points in the map.

 private List<Polyline> polylines;
private static final int[] COLORS = new int[]{R.color.primary_dark_material_light};
@Override
public void onRoutingSuccess(ArrayList<Route> route, int shortestRouteIndex) {
    if(polylines.size()>0) {
        for (Polyline poly : polylines) {
            poly.remove();
        }
    }

    polylines = new ArrayList<>();
    for (int i = 0; i <route.size(); i++) {

        int colorIndex = i % COLORS.length;

        PolylineOptions polyOptions = new PolylineOptions();
        polyOptions.color(getResources().getColor(COLORS[colorIndex]));
        polyOptions.width(10 + i * 3);
        polyOptions.addAll(route.get(i).getPoints());
        Polyline polyline = mMap.addPolyline(polyOptions);
        polylines.add(polyline);

        TextView friendDist = (TextView) findViewById(R.id.distance);

        //following line will generate the distance
        friendDist.setText("Distance: "+ route.get(i).getDistanceValue() +" meters");

    }

}

Upvotes: 1

maheryhaja
maheryhaja

Reputation: 1687

you can use google map API for that. You will get response like this:

{
  "destination_addresses" : [ "New York, NY, USA" ],
  "origin_addresses" : [ "Washington, DC, USA" ],
  "rows" : [
    {
      "elements" : [
        {
          "distance" : {
            "text" : "225 mi",
            "value" : 361715
          },
          "duration" : {
            "text" : "3 hours 49 mins",
            "value" : 13725
          },
          "status" : "OK"
        }
      ]
    }
  ],
  "status" : "OK"
}

You can also use latitude and longitude if you want.

Upvotes: 2

Jay Thummar
Jay Thummar

Reputation: 2299

Use this code it will accept the starting point lat lng and destination point lat lng and retunrn distance in KM

 public static double calculateDistanceBetweenTwoPoints(double startLat, double startLong, double endLat, double endLong) {
    double earthRadiusKm = 6372.8; //Earth's Radius In kilometers
    double dLat = Math.toRadians(endLat - startLat);
    double dLon = Math.toRadians(endLong - startLong);
    startLat = Math.toRadians(startLat);
    endLat = Math.toRadians(endLat);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(startLat) * Math.cos(endLat);
    double c = 2 * Math.asin(Math.sqrt(a));
    double haverdistanceKM = earthRadiusKm * c;
    return haverdistanceKM;
}

Upvotes: 0

Related Questions