Directions(Sample)
Use the DirectionsService
to get route data and render it in the most simple way. Checkout the DirectionsService to learn more.
Demo
Examples
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// You need to replace the apiKey with yours28nextbillion.apiKey = "db20c37e79d4420f9b2f71a766cacb91";29var nbmap = new nextbillion.maps.Map({30container: document.getElementById("map"),31style: "https://api.nextbillion.io/maps/streets/style.json",32zoom: 10,33center: { lat: 34.08572, lng: -118.324569 }34});3536nbmap.on("load", function () {37const directionsService = new nextbillion.maps.DirectionsService();38// request the directions api39directionsService40.route({41origin: { lat: 34.06, lng: -118.424569 },42waypoints: [{ lat: 33.98, lng: -118.354569 }],43destination: { lat: 34.06, lng: -118.224569 }44})45.then((response) => {46// render the result47nbmap.map.addSource("route", {48type: "geojson",49data: {50type: "Feature",51properties: {},52geometry: {53type: "LineString",54coordinates: nextbillion.utils.polyline55.decode(response.routes[0].geometry, 6)56.map((c) => c.reverse())57}58}59});60nbmap.map.addLayer({61id: "route",62type: "line",63source: "route",64layout: {65"line-join": "round",66"line-cap": "round"67},68paint: {69"line-color": "#8D5A9E",70"line-width": 871}72});73});74});75})();76</script>77</body>78</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, DirectionsService } from '@nbai/nbmap-gl'45const DirectionsDemo = () => {6useEffect(() => {7nextbillion.apiKey = '<your_api_key>'8const nbmap = new NBMap({9container: document.getElementById('map'),10style: 'https://api.nextbillion.io/maps/streets/style.json',11zoom: 10,12center: { lat: 34.08572, lng: -118.324569 }13})1415nbmap.on('load', function () {16const directionsService = new DirectionsService()17// request the directions api18directionsService19.route({20origin: { lat: 34.06, lng: -118.424569 },21waypoints: [{ lat: 33.98, lng: -118.354569 }],22destination: { lat: 34.06, lng: -118.224569 }23})24.then((response) => {25// render the result26nbmap.map.addSource('route', {27type: 'geojson',28data: {29type: 'Feature',30properties: {},31geometry: {32type: 'LineString',33coordinates: nextbillion.utils.polyline34.decode(response.routes[0].geometry, 6)35.map((c) => c.reverse())36}37}38})39nbmap.map.addLayer({40id: 'route',41type: 'line',42source: 'route',43layout: {44'line-join': 'round',45'line-cap': 'round'46},47paint: {48'line-color': '#8D5A9E',49'line-width': 850}51})52})53})54})5556return (57<div className="app">58<div59style={{60width: '100%',61height: '100%',62position: 'fixed',63top: '0',64left: '0'65}}66id="map"></div>67</div>68)69}70export default DirectionsDemo71