Shape & Symbol
Draw different shapes and symbol 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 () {27// draw a polyline28function drawPolyline(nbmap) {29nbmap.map.addSource("polyline-source", {30type: "geojson",31data: {32type: "FeatureCollection",33features: [34{35type: "Feature",36properties: {37color: "#e74747" // red clolor38},39geometry: {40type: "LineString",41coordinates: [42[-118.24648, 34.05815],43[-118.24556, 34.05739],44[-118.2445, 34.0565],45[-118.24397, 34.05605],46[-118.24359, 34.05573],47[-118.24372, 34.05559],48[-118.24385, 34.05544],49[-118.24427, 34.05501],50[-118.24509, 34.05412],51[-118.24555, 34.05361]52]53}54},55{56type: "Feature",57properties: {58color: "#93a1ff" // blue clolor59},60geometry: {61type: "LineString",62coordinates: [63[-118.24648, 34.05815],64[-118.24748, 34.05725],65[-118.24763, 34.05712],66[-118.24797, 34.05678],67[-118.24823, 34.05641],68[-118.24836, 34.05623],69[-118.24874572, 34.05567155],70[-118.24637, 34.05417],71[-118.24571, 34.05376],72[-118.24553, 34.05363],73[-118.24555, 34.05361]74]75}76}77]78}79});8081// you can get more information about setting the style of lines here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/#line82nbmap.map.addLayer({83id: "polyline-layer",84type: "line",85source: "polyline-source",86paint: {87"line-width": 3,88// get color from the source89"line-color": ["get", "color"]90}91});92}9394// draw a polygon95function drawPolygon(nbmap) {96nbmap.map.addSource("polygon-source", {97type: "geojson",98data: {99type: "FeatureCollection",100features: [101{102type: "Feature",103geometry: {104type: "Polygon",105coordinates: [106[107[-118.24455192, 34.05649731],108[-118.24646327, 34.05811205],109[-118.24751855, 34.05716641],110[-118.2479062, 34.05678279],111[-118.24834231, 34.05621183],112[-118.24870843, 34.05567655],113[-118.24661649, 34.05436717],114[-118.24455192, 34.05649731]115]116]117}118}119]120}121});122123// you can get more information about setting the style of layers here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/124nbmap.map.addLayer({125id: "polygon-layer",126type: "fill",127source: "polygon-source",128paint: {129// fill with green color and a opacity of 0.6130"fill-color": "#469300",131"fill-opacity": 0.6132}133});134}135136// draw a circle137function drawCircle(nbmap) {138nbmap.map.addSource("circle-source", {139type: "geojson",140data: {141type: "FeatureCollection",142features: [143{144type: "Feature",145geometry: {146type: "Point",147coordinates: [-118.24501946, 34.05507342]148}149}150]151}152});153// you can get more information about setting the style of layers here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/#circle154nbmap.map.addLayer({155id: "circle-layer",156type: "circle",157source: "circle-source",158paint: {159"circle-color": "#8d5a9e",160"circle-radius": 40161}162});163}164165// draw symbol166function drawSymbol(nbmap) {167nbmap.map.addSource("symbol-source", {168type: "geojson",169data: {170type: "FeatureCollection",171features: [172{173type: "Feature",174geometry: {175type: "Point",176coordinates: [-118.24501946, 34.05507342]177}178}179]180}181});182// you can get more information about setting the style of layers here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/#symbol183nbmap.map.addLayer({184id: "symbol-layer",185type: "symbol",186source: "symbol-source",187layout: {188"symbol-placement": "point",189visibility: "visible",190"symbol-spacing": 1,191"icon-allow-overlap": true,192"icon-image": "nb-logo",193"icon-size": 0.16194}195});196}197198// You need to replace the apiKey with yours199nextbillion.setApiKey("your-api-key");200var nbmap = new nextbillion.maps.Map({201container: document.getElementById("map"),202style: "https://api.nextbillion.io/maps/streets/style.json",203zoom: 15,204center: { lat: 34.05649731, lng: -118.24455192 }205});206207nbmap.on("load", () => {208drawPolyline(nbmap);209drawPolygon(nbmap);210drawCircle(nbmap);211// load image so we can use it in the symbol layer212nbmap.map.loadImage(213"https://static.nextbillion.io/docs-next/nbai-logo-white.png",214(err, image) => {215if (err) {216console.error("err image", err);217return;218}219nbmap.map.addImage("nb-logo", image);220drawSymbol(nbmap);221}222);223});224})();225</script>226</body>227</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 } from '@nbai/nbmap-gl'45// draw a polyline6function drawPolyline(nbmap) {7nbmap.map.addSource('polyline-source', {8type: 'geojson',9data: {10type: 'FeatureCollection',11features: [12{13type: 'Feature',14properties: {15color: '#e74747' // red clolor16},17geometry: {18type: 'LineString',19coordinates: [20[-118.24648, 34.05815],21[-118.24556, 34.05739],22[-118.2445, 34.0565],23[-118.24397, 34.05605],24[-118.24359, 34.05573],25[-118.24372, 34.05559],26[-118.24385, 34.05544],27[-118.24427, 34.05501],28[-118.24509, 34.05412],29[-118.24555, 34.05361]30]31}32},33{34type: 'Feature',35properties: {36color: '#93a1ff' // blue clolor37},38geometry: {39type: 'LineString',40coordinates: [41[-118.24648, 34.05815],42[-118.24748, 34.05725],43[-118.24763, 34.05712],44[-118.24797, 34.05678],45[-118.24823, 34.05641],46[-118.24836, 34.05623],47[-118.24874572, 34.05567155],48[-118.24637, 34.05417],49[-118.24571, 34.05376],50[-118.24553, 34.05363],51[-118.24555, 34.05361]52]53}54}55]56}57})5859// you can get more information about setting the style of lines here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/#line60nbmap.map.addLayer({61id: 'polyline-layer',62type: 'line',63source: 'polyline-source',64paint: {65'line-width': 3,66// get color from the source67'line-color': ['get', 'color']68}69})70}7172// draw a polygon73function drawPolygon(nbmap) {74nbmap.map.addSource('polygon-source', {75type: 'geojson',76data: {77type: 'FeatureCollection',78features: [79{80type: 'Feature',81geometry: {82type: 'Polygon',83coordinates: [84[85[-118.24455192, 34.05649731],86[-118.24646327, 34.05811205],87[-118.24751855, 34.05716641],88[-118.2479062, 34.05678279],89[-118.24834231, 34.05621183],90[-118.24870843, 34.05567655],91[-118.24661649, 34.05436717],92[-118.24455192, 34.05649731]93]94]95}96}97]98}99})100101// you can get more information about setting the style of layers here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/102nbmap.map.addLayer({103id: 'polygon-layer',104type: 'fill',105source: 'polygon-source',106paint: {107// fill with green color and a opacity of 0.6108'fill-color': '#469300',109'fill-opacity': 0.6110}111})112}113114// draw a circle115function drawCircle(nbmap) {116nbmap.map.addSource('circle-source', {117type: 'geojson',118data: {119type: 'FeatureCollection',120features: [121{122type: 'Feature',123geometry: {124type: 'Point',125coordinates: [-118.24501946, 34.05507342]126}127}128]129}130})131// you can get more information about setting the style of layers here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/#circle132nbmap.map.addLayer({133id: 'circle-layer',134type: 'circle',135source: 'circle-source',136paint: {137'circle-color': '#8d5a9e',138'circle-radius': 40139}140})141}142143// draw symbol144function drawSymbol(nbmap) {145nbmap.map.addSource('symbol-source', {146type: 'geojson',147data: {148type: 'FeatureCollection',149features: [150{151type: 'Feature',152geometry: {153type: 'Point',154coordinates: [-118.24501946, 34.05507342]155}156}157]158}159})160// you can get more information about setting the style of layers here. https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/#symbol161nbmap.map.addLayer({162id: 'symbol-layer',163type: 'symbol',164source: 'symbol-source',165layout: {166'symbol-placement': 'point',167visibility: 'visible',168'symbol-spacing': 1,169'icon-allow-overlap': true,170'icon-image': 'nb-logo',171'icon-size': 0.16172}173})174}175176const DrawDemo = () => {177useEffect(() => {178nextbillion.setApiKey('your-api-key')179const nbmap = new NBMap({180container: 'map',181style: 'https://api.nextbillion.io/maps/streets/style.json',182zoom: 15,183center: { lat: 34.05649731, lng: -118.24455192 }184})185186nbmap.on('load', () => {187drawPolyline(nbmap)188drawPolygon(nbmap)189drawCircle(nbmap)190// load image so we can use it in the symbol layer191nbmap.map.loadImage(192'https://static.nextbillion.io/docs-next/nbai-logo-white.png',193(err, image) => {194if (err) {195console.error('err image', err)196return197}198nbmap.map.addImage('nb-logo', image)199drawSymbol(nbmap)200}201)202})203}, [])204205return (206<div className="app">207<div208style={{209width: '100%',210height: '100%',211position: 'fixed',212top: '0',213left: '0'214}}215id="map"></div>216</div>217)218}219export default DrawDemo