距离矩阵api和方向api之间的差异

时间:2017-04-12 08:49:31

标签: android google-maps-api-2 google-distancematrix-api google-maps-direction-api

我想在谷歌地图上显示路线,我知道使用api的方向。我想要两个点之间的持续时间和距离,这也是我们可以从方向api得到的。但下一步是计算位置距离和持续时间,因为A点的一个人连续向B点移动,然后我只想要持续时间和距离。所以问题是......

1)获得连续的持续时间和距离没有使用api的路由信息​​?距离矩阵或方向api?他们之间有什么区别?我现在正在使用方向api。我应该使用距离矩阵api吗?

2)我正在使用diraction api而不传递密钥...... https://maps.googleapis.com/maps/api/directions/json?origin=23.0509738,72.5193273&destination=23.046219,72.515696&sensor=false&units=metric&mode=driving它给出了我在示例中看到的相同结果。所以我不使用密钥就使用它?或者我必须将密钥与url一起传递?

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

0 个答案:

没有答案