Shape & Symbol

Draw different shapes and symbol to the map.

Demo

Plain HTML

You can find the live Demo on CodeSandbox here.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta name="viewport" content="initial-scale=1.0" />
    <meta charset="UTF-8" />
    <title>Basic Map</title>
    <link
      href="https://maps-gl.nextbillion.io/maps/v2/api/css"
      rel="stylesheet"
    />
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #map {
        width: 100vw;
        height: 100vh;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script src="https://maps-gl.nextbillion.io/maps/v2/api/js"></script>
    <script>
      (function () {
        // draw a polyline
        function drawPolyline(nbmap) {
          nbmap.map.addSource("polyline-source", {
            type: "geojson",
            data: {
              type: "FeatureCollection",
              features: [
                {
                  type: "Feature",
                  properties: {
                    color: "#e74747" // red clolor
                  },
                  geometry: {
                    type: "LineString",
                    coordinates: [
                      [-118.24648, 34.05815],
                      [-118.24556, 34.05739],
                      [-118.2445, 34.0565],
                      [-118.24397, 34.05605],
                      [-118.24359, 34.05573],
                      [-118.24372, 34.05559],
                      [-118.24385, 34.05544],
                      [-118.24427, 34.05501],
                      [-118.24509, 34.05412],
                      [-118.24555, 34.05361]
                    ]
                  }
                },
                {
                  type: "Feature",
                  properties: {
                    color: "#93a1ff" // blue clolor
                  },
                  geometry: {
                    type: "LineString",
                    coordinates: [
                      [-118.24648, 34.05815],
                      [-118.24748, 34.05725],
                      [-118.24763, 34.05712],
                      [-118.24797, 34.05678],
                      [-118.24823, 34.05641],
                      [-118.24836, 34.05623],
                      [-118.24874572, 34.05567155],
                      [-118.24637, 34.05417],
                      [-118.24571, 34.05376],
                      [-118.24553, 34.05363],
                      [-118.24555, 34.05361]
                    ]
                  }
                }
              ]
            }
          });

          // you can get more information about setting the style of lines here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/#line
          nbmap.map.addLayer({
            id: "polyline-layer",
            type: "line",
            source: "polyline-source",
            paint: {
              "line-width": 3,
              // get color from the source
              "line-color": ["get", "color"]
            }
          });
        }

        // draw a polygon
        function drawPolygon(nbmap) {
          nbmap.map.addSource("polygon-source", {
            type: "geojson",
            data: {
              type: "FeatureCollection",
              features: [
                {
                  type: "Feature",
                  geometry: {
                    type: "Polygon",
                    coordinates: [
                      [
                        [-118.24455192, 34.05649731],
                        [-118.24646327, 34.05811205],
                        [-118.24751855, 34.05716641],
                        [-118.2479062, 34.05678279],
                        [-118.24834231, 34.05621183],
                        [-118.24870843, 34.05567655],
                        [-118.24661649, 34.05436717],
                        [-118.24455192, 34.05649731]
                      ]
                    ]
                  }
                }
              ]
            }
          });

          // you can get more information about setting the style of layers here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/
          nbmap.map.addLayer({
            id: "polygon-layer",
            type: "fill",
            source: "polygon-source",
            paint: {
              // fill with green color and a opacity of 0.6
              "fill-color": "#469300",
              "fill-opacity": 0.6
            }
          });
        }

        // draw a circle
        function drawCircle(nbmap) {
          nbmap.map.addSource("circle-source", {
            type: "geojson",
            data: {
              type: "FeatureCollection",
              features: [
                {
                  type: "Feature",
                  geometry: {
                    type: "Point",
                    coordinates: [-118.24501946, 34.05507342]
                  }
                }
              ]
            }
          });
          // you can get more information about setting the style of layers here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/#circle
          nbmap.map.addLayer({
            id: "circle-layer",
            type: "circle",
            source: "circle-source",
            paint: {
              "circle-color": "#8d5a9e",
              "circle-radius": 40
            }
          });
        }

        // draw symbol
        function drawSymbol(nbmap) {
          nbmap.map.addSource("symbol-source", {
            type: "geojson",
            data: {
              type: "FeatureCollection",
              features: [
                {
                  type: "Feature",
                  geometry: {
                    type: "Point",
                    coordinates: [-118.24501946, 34.05507342]
                  }
                }
              ]
            }
          });
          // you can get more information about setting the style of layers here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/#symbol
          nbmap.map.addLayer({
            id: "symbol-layer",
            type: "symbol",
            source: "symbol-source",
            layout: {
              "symbol-placement": "point",
              visibility: "visible",
              "symbol-spacing": 1,
              "icon-allow-overlap": true,
              "icon-image": "nb-logo",
              "icon-size": 0.16
            }
          });
        }

        // You need to replace the apiKey with yours
        nextbillion.setApiKey("your-api-key");
        var nbmap = new nextbillion.maps.Map({
          container: document.getElementById("map"),
          style: "https://api.nextbillion.io/maps/streets/style.json",
          zoom: 15,
          center: { lat: 34.05649731, lng: -118.24455192 }
        });

        nbmap.on("load", () => {
          drawPolyline(nbmap);
          drawPolygon(nbmap);
          drawCircle(nbmap);
          // load image so we can use it in the symbol layer
          nbmap.map.loadImage(
            "https://static.nextbillion.io/docs-next/nbai-logo-white.png",
            (err, image) => {
              if (err) {
                console.error("err image", err);
                return;
              }
              nbmap.map.addImage("nb-logo", image);
              drawSymbol(nbmap);
            }
          );
        });
      })();
    </script>
  </body>
</html>

React

You can find the live Demo on CodeSandbox here.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import React, { useEffect } from 'react'
import '@nbai/nbmap-gl/dist/nextbillion.css'
import nextbillion, { NBMap } from '@nbai/nbmap-gl'

// draw a polyline
function drawPolyline(nbmap) {
  nbmap.map.addSource('polyline-source', {
    type: 'geojson',
    data: {
      type: 'FeatureCollection',
      features: [
        {
          type: 'Feature',
          properties: {
            color: '#e74747' // red clolor
          },
          geometry: {
            type: 'LineString',
            coordinates: [
              [-118.24648, 34.05815],
              [-118.24556, 34.05739],
              [-118.2445, 34.0565],
              [-118.24397, 34.05605],
              [-118.24359, 34.05573],
              [-118.24372, 34.05559],
              [-118.24385, 34.05544],
              [-118.24427, 34.05501],
              [-118.24509, 34.05412],
              [-118.24555, 34.05361]
            ]
          }
        },
        {
          type: 'Feature',
          properties: {
            color: '#93a1ff' // blue clolor
          },
          geometry: {
            type: 'LineString',
            coordinates: [
              [-118.24648, 34.05815],
              [-118.24748, 34.05725],
              [-118.24763, 34.05712],
              [-118.24797, 34.05678],
              [-118.24823, 34.05641],
              [-118.24836, 34.05623],
              [-118.24874572, 34.05567155],
              [-118.24637, 34.05417],
              [-118.24571, 34.05376],
              [-118.24553, 34.05363],
              [-118.24555, 34.05361]
            ]
          }
        }
      ]
    }
  })

  // you can get more information about setting the style of lines here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/#line
  nbmap.map.addLayer({
    id: 'polyline-layer',
    type: 'line',
    source: 'polyline-source',
    paint: {
      'line-width': 3,
      // get color from the source
      'line-color': ['get', 'color']
    }
  })
}

// draw a polygon
function drawPolygon(nbmap) {
  nbmap.map.addSource('polygon-source', {
    type: 'geojson',
    data: {
      type: 'FeatureCollection',
      features: [
        {
          type: 'Feature',
          geometry: {
            type: 'Polygon',
            coordinates: [
              [
                [-118.24455192, 34.05649731],
                [-118.24646327, 34.05811205],
                [-118.24751855, 34.05716641],
                [-118.2479062, 34.05678279],
                [-118.24834231, 34.05621183],
                [-118.24870843, 34.05567655],
                [-118.24661649, 34.05436717],
                [-118.24455192, 34.05649731]
              ]
            ]
          }
        }
      ]
    }
  })

  // you can get more information about setting the style of layers here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/
  nbmap.map.addLayer({
    id: 'polygon-layer',
    type: 'fill',
    source: 'polygon-source',
    paint: {
      // fill with green color and a opacity of 0.6
      'fill-color': '#469300',
      'fill-opacity': 0.6
    }
  })
}

// draw a circle
function drawCircle(nbmap) {
  nbmap.map.addSource('circle-source', {
    type: 'geojson',
    data: {
      type: 'FeatureCollection',
      features: [
        {
          type: 'Feature',
          geometry: {
            type: 'Point',
            coordinates: [-118.24501946, 34.05507342]
          }
        }
      ]
    }
  })
  // you can get more information about setting the style of layers here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/#circle
  nbmap.map.addLayer({
    id: 'circle-layer',
    type: 'circle',
    source: 'circle-source',
    paint: {
      'circle-color': '#8d5a9e',
      'circle-radius': 40
    }
  })
}

// draw symbol
function drawSymbol(nbmap) {
  nbmap.map.addSource('symbol-source', {
    type: 'geojson',
    data: {
      type: 'FeatureCollection',
      features: [
        {
          type: 'Feature',
          geometry: {
            type: 'Point',
            coordinates: [-118.24501946, 34.05507342]
          }
        }
      ]
    }
  })
  // you can get more information about setting the style of layers here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/#symbol
  nbmap.map.addLayer({
    id: 'symbol-layer',
    type: 'symbol',
    source: 'symbol-source',
    layout: {
      'symbol-placement': 'point',
      visibility: 'visible',
      'symbol-spacing': 1,
      'icon-allow-overlap': true,
      'icon-image': 'nb-logo',
      'icon-size': 0.16
    }
  })
}

const DrawDemo = () => {
  useEffect(() => {
    nextbillion.setApiKey('your-api-key')
    const nbmap = new NBMap({
      container: 'map',
      style: 'https://api.nextbillion.io/maps/streets/style.json',
      zoom: 15,
      center: { lat: 34.05649731, lng: -118.24455192 }
    })

    nbmap.on('load', () => {
      drawPolyline(nbmap)
      drawPolygon(nbmap)
      drawCircle(nbmap)
      // load image so we can use it in the symbol layer
      nbmap.map.loadImage(
        'https://static.nextbillion.io/docs-next/nbai-logo-white.png',
        (err, image) => {
          if (err) {
            console.error('err image', err)
            return
          }
          nbmap.map.addImage('nb-logo', image)
          drawSymbol(nbmap)
        }
      )
    })
  }, [])

  return (
    <div className="app">
      <div
        style={{
          width: '100%',
          height: '100%',
          position: 'fixed',
          top: '0',
          left: '0'
        }}
        id="map"></div>
    </div>
  )
}
export default DrawDemo


Have Questions ?