• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

API - Directions

A newer version of Maps for web SDK is available
A newer version of the SDK is available and is recommended for all users. Learn about the latest version, V2.0, in the Maps for web v2.0 documentation

With NextBillion.ai Maps API, you can access all kinds of services from the map data to support your business, with just a few lines of code.

In this section, we'll introduce the Directions API.

The Directions API takes 2 locations as the origin and the destination, and returns the directions between them. You can set various traveling modes for a more accurate directions result.

You can use the SDK's built-in methods to request and render the result. Also, it's your freedom to manually process and visualize the data when needed.

There are detailed instructions in the references of Directions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>NBMap Basic Example</title>
    <link href="https://maps-gl.nextbillion.io/maps/api/css" rel="stylesheet" />
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #map {
        width: 100vw;
        height: 100vh;
      }
      #add-route {
        position: absolute;
        top: 20px;
        right: 20px;
        padding: 12px;
        z-index: 99;
      }
      #remove-route {
        position: absolute;
        top: 70px;
        right: 20px;
        padding: 12px;
        z-index: 99;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <button id="add-route">Request Direction</button>
    <button id="remove-route">Remove Direction</button>

    <script src="https://maps-gl.nextbillion.io/maps/api/js"></script>
    <script>
      ;(function () {
        nextbillion.setApiKey('your-api-key')
        // Optional: you can set the API host if it differs from "api.nextbillion.io"
        nextbillion.setApiHost('your-api-endpoint')
        var map = new nextbillion.maps.Map(document.getElementById('map'), {
          zoom: 12,
          center: { lat: 28.6139, lng: 77.209 },
        })

        var directionShapes = null
        document.getElementById('add-route').onclick = async function () {
          if (directionShapes) {
            return
          }

          var resp = await nextbillion.api.Directions({
            origin: { lat: 24.9048329, lng: 74.5865747 },
            destination: { lat: 26.82585465, lng: 75.80178009 },
          })

          directionShapes = map.renderDirections(resp)
        }

        document.getElementById('remove-route').onclick = function () {
          if (!directionShapes) {
            return
          }
          map.removeDirections(directionShapes)
          directionShapes = null
        }
      })()
    </script>
  </body>
</html>