0

我正在直接从文档中尝试这个 URL,它工作正常,显示了一条由可步行部分以及公共交通工具组成的部分组成的路线:

https://transit.router.hereapi.com/v8/routes?apiKey={key}&origin=41.79457,12.25473&destination=41.90096,12.50243

但是,当我在另一条路线上尝试此操作时,它说没有可用的路线:

https://transit.router.hereapi.com/v8/routes?apiKey={key}&origin=22.50,88.36&destination=22.64,88.43

顺便说一句,这两个坐标是印度加尔各答的两个地方——一个人可以用谷歌搜索它们(Jodhpur Park coordinatesDum Dum coordinates),谷歌实际上提供了这两个地方之间的公共交通,这表明这两个地方之间确实存在公共交通:

在此处输入图像描述

谁能告诉我如何让 Here API 返回这些路线的公共交通数据?或者有没有公共交通数据不可用的路线,即使是在加尔各答这样的大都市?

4

1 回答 1

1

TL;博士

如果您在第二个 API 请求坐标中包含更多数字,与第一个请求中的数字一样多,这应该可以解决您的问题。

完整的 URL (带有排除的 API 密钥)是:

https://transit.router.hereapi.com/v8/routes?apiKey={key}&origin=22.5058,88.3640&destination=22.6420,88.4312

我在下面的详细答案下面有一个现场演示,您可以在其中粘贴您的 API 密钥并运行查询以查看此操作。

详细解答

在您的第二个示例中,您的纬度和经度坐标需要更具体,因为它们与您正在寻找往返路线的实际位置不匹配。

而不是使用

origin=22.50,88.36&destination=22.64,88.43

使用✅</p>

origin=22.5058,88.3640&destination=22.6420,88.4312

我从 Google 中提取了这两个位置的坐标……</p>

搜索“焦特布尔公园坐标”:

焦特布尔公园坐标

搜索“Dum Dum 坐标”:

达姆达姆坐标

完整的 URL (带有排除的 API 密钥)是:

https://transit.router.hereapi.com/v8/routes?apiKey={key}&origin=22.5058,88.3640&destination=22.6420,88.4312

生成的 JSON 数据会生成三种不同的运输路线。

现场演示

这是一个实际演示,您可以在其中安全地使用自己的 API 密钥,并根据需要调整起点和目的地 lat/lng 坐标以从 API 生成数据:

const getById = id => document.getElementById(id),
      apiKey  = getById('api-key'),
      origLat = getById('orig-lat'),
      origLng = getById('orig-lng'),
      destLat = getById('dest-lat'),
      destLng = getById('dest-lng'),
      button  = getById('submit'),
      output  = getById('output');
const getDirections = () => {
  fetch(`https://transit.router.hereapi.com/v8/routes?apiKey=${apiKey}&origin=${origLat},${origLng}&destination=${destLat},${destLng}`).then(res => res.json()).then(data => output.innerHTML = JSON.stringify(data, null, 2)).catch(error => output.innerHTML = error);
};
[apiKey, origLat, origLng, destLat, destLng].forEach(field => field.toString = () => field.value);
button.addEventListener('click', getDirections);
html {
  height: 100%;
  box-sizing: border-box;
  font-family: consolas, monospace;
}
*, *::before, *::after {
  box-sizing: inherit;
  font: inherit;
}
body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: stretch;
  gap: 20px;
  min-height: 100%;
  margin: 0;
  font-smooth: antialiased;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
    overflow: hidden scroll;
    -ms-overflow-style: none;
    scrollbar-width: none;
}
body::-webkit-scrollbar {
  width: 0;
  display: none;
}
h2 {
  width: 100%;
  margin: 0;
  padding: 10px 20px;
  background-color: #000;
  font-size: 120%;
  font-weight: 700;
  color: #fff;
  text-align: center;
}
#fields {
  display: flex;
  flex-direction: column;
  gap: 5px;
}
label {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 100%;
  max-width: 300px;
}
input {
  appearance: none;
  -webkit-appearance: none;
  width: 100px;
  margin-left: 5px;
  padding: 5px 10px;
  background-color: #ddd;
  border: none;
  text-align: right;
}
pre {
  width: 100%;
  padding: 10px;
  border-top: 1px solid #000;
  flex: 1;
  margin: 0;
  overflow-y: hidden;
  background-color: #ddd;
  font-size: 85%;
}
<h2>Here API demo</h2>
<div id="fields">
  <label>API Key<input id="api-key" placeholder="API key"></label>
  <label>Origin Latitude<input id="orig-lat" value="22.5058"></label>
  <label>Origin Longitude<input id="orig-lng" value="88.3640"></label>
  <label>Destination Latitude<input id="dest-lat" value="22.6420"></label>
  <label>Destination Longitude<input id="dest-lng" value="88.4312"></label>
</div>
<button id="submit">Run a query</button>
<pre id="output">Run a query using the button above to load the results here.</pre>

于 2021-09-17T19:19:56.470 回答