0

I am trying this URL straight from documentation, and it works fine, showing a route consisting of walkable sections as well as sections to be plied by public transport:

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

However, when I try this on another route, it says that no route is available:

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

The two coordinates are incidentally, two places in Kolkata, a city in India - one can just Google them (Jodhpur Park coordinates and Dum Dum coordinates) and Google actually offers a public transit between those two places, suggesting that public transit does exist between those two places:

enter image description here

Can anyone tell me how to make Here API return the public transit data for these route? Or are there routes where public transit data is not available, even in an otherwise metro city like Kolkata?

SexyBeast
  • 7,913
  • 28
  • 108
  • 196
  • 1
    Please see my answer when you have a chance. It appears to be a matter of specificity. If you input your API key into the live demo, you can see it returns many routes, including transit, between Jodhpur Park and Dum Dum. We just need to include more digits in our coordinates. – Brandon McConnell Sep 17 '21 at 20:27

1 Answers1

1

TL;DR

If you include more digits in your second API request coordinates, as many as you have in your first request, this should resolve your issue.

The full URL (with excluded API key) is:

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

I have a live demo of this below the detailed answer below where you can paste in your API key and run the query to see this in action.

Detailed answer

In your second example, your latitude and longitude coordinates need to be more specific, as they do not match the actual locations you are looking for transit directions from/to.

Instead of using

origin=22.50,88.36&destination=22.64,88.43

Use ✅

origin=22.5058,88.3640&destination=22.6420,88.4312

I pulled these coordinates from Google for both locations…

Searching "Jodhpur Park coordinates":

Jodhpur Park coordinates

Searching "Dum Dum coordinates":

Dum Dum coordinates

The full URL (with excluded API key) is:

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

The resulting JSON data produces three different transit routes.

LIVE DEMO

Here is a demo of this in action where you can securely use your own API key as well as adjust the Origin and Destination lat/lng coordinates as needed to generate data from the 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>
Brandon McConnell
  • 5,776
  • 1
  • 20
  • 36
  • Wow. That is neat. But one question, why is this specificity needed for certain places, and not for other? is it because some places (like places in USA) are mapped in detail, and some (like in India) are not mapped as thoroughly? – SexyBeast Sep 17 '21 at 20:38
  • 1
    @SexyBeast The extra digits are not required, but without specificity, a maps engine might land your client in the middle of the desert, on a mountain top, or in the middle of the ocean. In this particular example, the first coordinate is in the middle of the park. Google Maps intelligently knows to find the nearest bus stop for you and use that instead, but HereAPI may not have that same feature built-in. Definitely worth opening a support ticket with them for this. They may be able to add this feature or resolve this issue. Tweet them here: https://twitter.com/here – Brandon McConnell Sep 17 '21 at 20:46
  • @SexyBeast Sure thing! – Brandon McConnell Sep 17 '21 at 20:59