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<link8href="https://maps-gl.nextbillion.io/maps/v2/api/css"9rel="stylesheet"10/>11<style>12* {13margin: 0;14padding: 0;15}16#map {17width: 100vw;18height: 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 () {27function addSimpleMarker(nbmap) {28const popup = new nextbillion.maps.Popup({29offset: 25,30closeButton: false31}).setText("Simple Marker");32const marker = new nextbillion.maps.Marker()33.setLngLat({ lat: 34.08572, lng: -118.124569 })34.setPopup(popup)35.addTo(nbmap.map);36marker.togglePopup();37}3839function addDraggableMarker(nbmap) {40const popup = new nextbillion.maps.Popup({41offset: 25,42closeButton: false43}).setText("Draggable Marker");44const marker = new nextbillion.maps.Marker({45draggable: true46})47.setLngLat({ lat: 34.08572, lng: -118.224569 })48.setPopup(popup)49.addTo(nbmap.map);50marker.togglePopup();51marker.on("dragend", () => {52console.log(marker.getLngLat());53});54}5556function addCustomMarkerWithDom(nbmap) {57const popup = new nextbillion.maps.Popup({58offset: 10,59closeButton: false60}).setText("Custom Marker");61const el = document.createElement("div");62el.className = "marker";63el.style.backgroundImage =64"url(https://static.nextbillion.io/docs-next/navigation-arrow-icon.png)";65el.style.backgroundSize = "cover";66el.style.width = "36px";67el.style.height = "36px";68const marker = new nextbillion.maps.Marker({69element: el70})71.setLngLat({ lat: 34.08572, lng: -118.024569 })72.setPopup(popup)73.addTo(nbmap.map);7475marker.togglePopup();76}77// You need to replace the apiKey with yours78nextbillion.setApiKey("your-api-key");79var nbmap = new nextbillion.maps.Map({80container: document.getElementById("map"),81style: "https://api.nextbillion.io/maps/streets/style.json",82zoom: 11,83center: { lat: 34.08572, lng: -118.324569 }84});8586nbmap.on("load", function () {87addSimpleMarker(nbmap);88addDraggableMarker(nbmap);89addCustomMarkerWithDom(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'45function addSimpleMarker(nbmap) {6const popup = new Popup({ offset: 25, closeButton: false }).setText(7'Simple Marker'8)9const marker = new Marker()10.setLngLat({ lat: 34.08572, lng: -118.124569 })11.setPopup(popup)12.addTo(nbmap.map)13marker.togglePopup()14}1516function addDraggableMarker(nbmap) {17const popup = new Popup({ offset: 25, closeButton: false }).setText(18'Draggable Marker'19)20const marker = new Marker({21draggable: true22})23.setLngLat({ lat: 34.08572, lng: -118.224569 })24.setPopup(popup)25.addTo(nbmap.map)26marker.togglePopup()27marker.on('dragend', () => {28console.log(marker.getLngLat())29})30}3132function addCustomMarkerWithDom(nbmap) {33const popup = new Popup({ offset: 10, closeButton: false }).setText(34'Custom Marker'35)36const el = document.createElement('div')37el.className = 'marker'38el.style.backgroundImage =39'url(https://static.nextbillion.io/docs-next/navigation-arrow-icon.png)'40el.style.backgroundSize = 'cover'41el.style.width = '36px'42el.style.height = '36px'43const marker = new Marker({44element: el45})46.setLngLat({ lat: 34.08572, lng: -118.024569 })47.setPopup(popup)48.addTo(nbmap.map)4950marker.togglePopup()51}5253const MarkerDemo = () => {54useEffect(() => {55// Add your apiKey56nextbillion.setApiKey('your-api-key')57const nbmap = new NBMap({58container: 'map',59style: 'https://api.nextbillion.io/maps/streets/style.json',60zoom: 11,61center: { lat: 34.08572, lng: -118.124569 }62})63addSimpleMarker(nbmap)64addDraggableMarker(nbmap)65addCustomMarkerWithDom(nbmap)66})6768return (69<div className="app">70<div71style={{72width: '100%',73height: '100%',74position: 'fixed',75top: '0',76left: '0'77}}78id="map"></div>79</div>80)81}82export default MarkerDemo