Marker

Add markers to the map.

Demo

Plain HTML

You can find the live Demo on CodeSandbox here.

1<!DOCTYPE html>
2<html lang="en">
3 <head>
4 <meta name="viewport" content="initial-scale=1.0" />
5 <meta charset="UTF-8" />
6 <title>Basic Map</title>
7 <link
8 href="https://maps-gl.nextbillion.io/maps/v2/api/css"
9 rel="stylesheet"
10 />
11 <style>
12 * {
13 margin: 0;
14 padding: 0;
15 }
16 #map {
17 width: 100vw;
18 height: 100vh;
19 }
20 </style>
21 </head>
22 <body>
23 <div id="map"></div>
24 <script src="https://maps-gl.nextbillion.io/maps/v2/api/js"></script>
25 <script>
26 (function () {
27 function addSimpleMarker(nbmap) {
28 const popup = new nextbillion.maps.Popup({
29 offset: 25,
30 closeButton: false
31 }).setText("Simple Marker");
32 const marker = new nextbillion.maps.Marker()
33 .setLngLat({ lat: 34.08572, lng: -118.124569 })
34 .setPopup(popup)
35 .addTo(nbmap.map);
36 marker.togglePopup();
37 }
38
39 function addDraggableMarker(nbmap) {
40 const popup = new nextbillion.maps.Popup({
41 offset: 25,
42 closeButton: false
43 }).setText("Draggable Marker");
44 const marker = new nextbillion.maps.Marker({
45 draggable: true
46 })
47 .setLngLat({ lat: 34.08572, lng: -118.224569 })
48 .setPopup(popup)
49 .addTo(nbmap.map);
50 marker.togglePopup();
51 marker.on("dragend", () => {
52 console.log(marker.getLngLat());
53 });
54 }
55
56 function addCustomMarkerWithDom(nbmap) {
57 const popup = new nextbillion.maps.Popup({
58 offset: 10,
59 closeButton: false
60 }).setText("Custom Marker");
61 const el = document.createElement("div");
62 el.className = "marker";
63 el.style.backgroundImage =
64 "url(https://static.nextbillion.io/docs-next/navigation-arrow-icon.png)";
65 el.style.backgroundSize = "cover";
66 el.style.width = "36px";
67 el.style.height = "36px";
68 const marker = new nextbillion.maps.Marker({
69 element: el
70 })
71 .setLngLat({ lat: 34.08572, lng: -118.024569 })
72 .setPopup(popup)
73 .addTo(nbmap.map);
74
75 marker.togglePopup();
76 }
77 // You need to replace the apiKey with yours
78 nextbillion.setApiKey("your-api-key");
79 var nbmap = new nextbillion.maps.Map({
80 container: document.getElementById("map"),
81 style: "https://api.nextbillion.io/maps/streets/style.json",
82 zoom: 11,
83 center: { lat: 34.08572, lng: -118.324569 }
84 });
85
86 nbmap.on("load", function () {
87 addSimpleMarker(nbmap);
88 addDraggableMarker(nbmap);
89 addCustomMarkerWithDom(nbmap);
90 });
91 })();
92 </script>
93 </body>
94</html>

React

You can find the live Demo on CodeSandbox here.

1import React, { useEffect } from 'react'
2import '@nbai/nbmap-gl/dist/nextbillion.css'
3import nextbillion, { NBMap, Marker, Popup } from '@nbai/nbmap-gl'
4
5function addSimpleMarker(nbmap) {
6 const popup = new Popup({ offset: 25, closeButton: false }).setText(
7 'Simple Marker'
8 )
9 const marker = new Marker()
10 .setLngLat({ lat: 34.08572, lng: -118.124569 })
11 .setPopup(popup)
12 .addTo(nbmap.map)
13 marker.togglePopup()
14}
15
16function addDraggableMarker(nbmap) {
17 const popup = new Popup({ offset: 25, closeButton: false }).setText(
18 'Draggable Marker'
19 )
20 const marker = new Marker({
21 draggable: true
22 })
23 .setLngLat({ lat: 34.08572, lng: -118.224569 })
24 .setPopup(popup)
25 .addTo(nbmap.map)
26 marker.togglePopup()
27 marker.on('dragend', () => {
28 console.log(marker.getLngLat())
29 })
30}
31
32function addCustomMarkerWithDom(nbmap) {
33 const popup = new Popup({ offset: 10, closeButton: false }).setText(
34 'Custom Marker'
35 )
36 const el = document.createElement('div')
37 el.className = 'marker'
38 el.style.backgroundImage =
39 'url(https://static.nextbillion.io/docs-next/navigation-arrow-icon.png)'
40 el.style.backgroundSize = 'cover'
41 el.style.width = '36px'
42 el.style.height = '36px'
43 const marker = new Marker({
44 element: el
45 })
46 .setLngLat({ lat: 34.08572, lng: -118.024569 })
47 .setPopup(popup)
48 .addTo(nbmap.map)
49
50 marker.togglePopup()
51}
52
53const MarkerDemo = () => {
54 useEffect(() => {
55 // Add your apiKey
56 nextbillion.setApiKey('your-api-key')
57 const nbmap = new NBMap({
58 container: 'map',
59 style: 'https://api.nextbillion.io/maps/streets/style.json',
60 zoom: 11,
61 center: { lat: 34.08572, lng: -118.124569 }
62 })
63 addSimpleMarker(nbmap)
64 addDraggableMarker(nbmap)
65 addCustomMarkerWithDom(nbmap)
66 })
67
68 return (
69 <div className="app">
70 <div
71 style={{
72 width: '100%',
73 height: '100%',
74 position: 'fixed',
75 top: '0',
76 left: '0'
77 }}
78 id="map"></div>
79 </div>
80 )
81}
82export default MarkerDemo