How to calculate on road distance between two points?

二次信任 提交于 2021-01-28 06:10:15

问题


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?


回答1:


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.




回答2:


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

    }

}



回答3:


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


来源:https://stackoverflow.com/questions/50922369/how-to-calculate-on-road-distance-between-two-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!