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



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

1)为了在没有路由信息的情况下获得连续的持续时间和距离,使用哪个API?距离矩阵还是方向 API?它们之间有什么区别?我现在正在使用方向 API。我应该使用距离矩阵 API 吗?

2)我正在使用目录 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;
}

当解决方案需要大量起点-终点之间的距离和行驶时间时,可以使用距离矩阵 API。

它们之间的区别在于">距离矩阵API计算任何给定的起点和目的地之间的距离和ETA。方向 API根据距离或 ETA 查找任意两个给定点之间的最佳路径。 因此,您需要根据需要使用这两个API。

阿拉伯数字。 在使用API的任何服务时,您必须指定API密钥。您必须使用 URL 传递密钥。

最新更新