here-apihere-maps-restheremapstransit

Here API does not show public transit for given route


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?


Solution

  • 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>