JS 11
we will start the Geocoding
and the directions API
The Mapbox Geocoding API allows us to forward geocoding and reverse geocoding:
Forward Geocoding is when we get the coordinates from a string:

The API returns a GeoJson feature array in Mapbox format:
We can filter the query with extra arguments:
mapboxClient1.geocoding
.forwardGeocode({
query: "colle", //string address we are querying
fuzzyMatch: false, //at false will search exact match query
autocomplete: true, //each typo will do one request to the API for closest
limit: 2, //default 5, max 10, number of features returned
language: ["de"], //language can prioritize translated places
proximity: [12.511, 41.891], // [][] for more, closest queries to the array
types: ["neighborhood"] //limits features for type
country: "Co" //uses the short ISO 3166 alpha 2 names, may be broken
})
//Place_types can be: country, region, district, place, locality, neighborhood, address, and poi
//poi being (Point of interest)
//region, neighborhood and place have bbox while points and poi don't
In Reverse Geocoding we get strings locations from coordinates:
The Mapbox Geocoder plugin adds a search bar from which to search places:
//The MapboxGeocoder object uses the Mapbox Geocoding API to enable places search.
mapboxgl.accessToken =
"pk.eyJ1IjoibWlzdGVybGludXgiLCJhIjoiY2tnams0OGtzMDhqejJ4bGxmdWhia255YSJ9.htJI3nLHJoB62eOycK9KMA";
const nolo= new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl,
})
mappe.addControl( nolo )
We can use the geocoder events to check the search results:
//the result event will trigger after the search
geocoder.on('result', (event) => {
console.log(event.result.center)
})
We can use the GeolocateControl for the geolocation API to locate the user:
//we can track current position/ AND SHOW heading(direction)
const direct= new mapboxgl.GeolocateControl({
positionOptions:{
enableHighAccuracy: true
},
trackUserLocation: true,
showUserHeading: true
})
//we can move the Geolocate
mappe.addControl(direct, "bottom-right")
Mapbox Direction API
To use the Direction API mapbox gives us, we can use the SDK node.js client:
//We already used the SDK in the Geocoder
mapboxgl.accessToken =
"pk.eyJ1IjoibWlzdGVybGludXgiLCJhIjoiY2tnams0OGtzMDhqejJ4bGxmdWhia255YSJ9.htJI3nLHJoB62eOycK9KMA";
//From the SDK we use the .direction and THEN we getDirection
const directionsClient = mapboxSdk({ accessToken: mapboxgl.accessToken });
directionsClient.directions
.getDirections({
profile: 'driving-traffic', //"driving" | "walking" | "cycling" movement options
steps: true, //default false, shows direction route steps
geometries: "geojson", //default "polyline", data type of directions
annotations : ["duration", "distance", "speed", "congestion"],
overview: "full", //we need both to get extra metadata, more steps
//we can put multiple direction coordinates
waypoints: [
{
coordinates: [12, 42],
},
{
coordinates: [12.01, 42.01]
}
]
})
.send()
.then( (response) => {
const directions = response.body; //routes[] are the directions
let route= directions.routes[0]
let punti= route.geometry.coordinates //array of each step coordinate
let dritte= route.legs[0].steps //for the text we need maneuver
let tripInstructions = '';
for (const step of dritte) {
tripInstructions += step.maneuver.instruction + " ,";
}
let tempo= route.duration //in minutes
let metri= route.distance //in meters
})
We can draw it using expressions and click over waypoints:
Last updated
Was this helpful?