【问题标题】:How to calculate on road distance between two points?如何计算两点之间的道路距离?
【发布时间】:2018-06-19 07:00:08
【问题描述】:

在我的 android 应用程序中,我计划为产品添加运费(每公里 X 美元)所以,我如何计算两点之间的道路距离?

我们的产品将从固定地点发货,但用户会给出目的地位置。

谁能给出详细的方法?

【问题讨论】:

  • 如果您不理解我的问题,请发表评论
  • 列出到目前为止你尝试过的代码。
  • 我不知道从哪里开始,我的意思是有不同的方法,比如 Jay 给出的第一个答案,还有其他使用 google maps API 的选项,所以我问这个问题是为了知道该怎么做它,我还没有编写任何代码。

标签: android google-maps


【解决方案1】:

您可以为此使用google map API。 你会得到这样的回应:

{
  "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"
}

您也可以根据需要使用纬度和经度。

【讨论】:

    【解决方案2】:

    如果您使用 Google 地图 API 并使用折线,只需调用 .getDistanceValue() 即可获取距离。下面的代码将向您展示如何在文本视图中显示距离值,计算地图中的 2 个点。

     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】:

      使用此代码它将接受起点 lat lng 和目的地点 lat lng 并以 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;
      }
      

      【讨论】:

      • 这会给出道路距离吗?(我不想要空中距离,这就是为什么)
      • 可能不是..因为你必须使用google api来计算距离
      猜你喜欢
      • 2021-04-30
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 2011-04-23
      相关资源
      最近更新 更多