【问题标题】:Calculate Road Distance for an array of longitude and latitudes计算经度和纬度数组的道路距离
【发布时间】:2016-06-22 07:35:16
【问题描述】:

我有一个 Android 应用,它每 15 秒捕获一次纬度,我们使用这组经度来查找起点和目的地之间的道路距离(按时间戳排序的第一个和最后一个经度)。

实际上,这个应用程序是为在城市中运行迷你卡车的客户而设计的,他们的卡车司机将使用这个应用程序来标记旅程的开始/结束、丢包等。我们将根据客户在运行期间的公里数收取费用一次旅行。

在我们的场景中,车辆将在充满转弯、环形交叉路口的殖民地/小区域运送数据包,并一次又一次地穿越同一条路径。

所以我试图找到一种最佳方法来计算与所有收集的经纬度的距离。

我选择了两种方法:

  1. 使用“haversine”公式计算两个纬度之间的地球距离,添加连续距离。在这里,我的距离缩短了 10%。
  2. 另一个是 google map api,但它在直路上成功,在我的场景中失败。

请提出一些方法,该方法可以提供至少 96-97% 的准确率,并且被业界广泛接受。我们不想在车辆中部署 VTS 设备,因为司机必须使用 Android 应用程序,这会使成本翻倍。

【问题讨论】:

    标签: android google-maps google-maps-android-api-2 haversine


    【解决方案1】:

    distanceTo() 方法将返回鸟飞时的距离,而不是公路。您可以使用此方法获得两点之间的距离(通过道路):

    public String getDistance(final double lat1, final double lon1, final double lat2, final double lon2){
    String parsedDistance;
    String response;
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
    
                    URL url = new URL("http://maps.googleapis.com/maps/api/directions/json?origin=" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric&mode=driving");
                    final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");
                    InputStream in = new BufferedInputStream(conn.getInputStream());
                    response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
    
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray array = jsonObject.getJSONArray("routes");
                    JSONObject routes = array.getJSONObject(0);
                    JSONArray legs = routes.getJSONArray("legs");
                    JSONObject steps = legs.getJSONObject(0);
                    JSONObject distance = steps.getJSONObject("distance");
                    parsedDistance=distance.getString("text");
    
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    thread.start();
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return parsedDistance;
    }
    

    【讨论】:

    • 我的问题是,我怎样才能为我自己的一组经纬度或一条特定的路径获得准确的距离,曲折弯弯?我已经尝试过这个解决方案作为一个选项,我的问题中也提到过。
    • 只要谷歌有地图,(它在路上等等)它应该适合你。如果 Google 没有绘制您的路线图,据我所知,我认为没有办法通过公路获得距离。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 2011-08-16
    • 2021-05-10
    • 1970-01-01
    相关资源
    最近更新 更多