Open In App

Node.js Mapbox Reverse Geocoding API

Reverse Geocoding: Reverse geocoding converts geographic coordinates into geographic features or features that exist at that location. For example, turning 22.7196, 75.8577 into ‘Indore’. Mapbox is popular for Geocoding API and other locations and map services. 

Feature of Mapbox Reverse Geocoding API:



Installation of request module:

You can visit the link to the Install Request module. You can install this package by using this command.



npm install request

After installing the request module you can check your request version in the command prompt using the command.

npm version request

Now go to Mapbox Official website and create an account and get your API KEY.

After that, you can create a folder and add a file, for example, index.js. To run this file you need to run the following command.

node index.js

Project Structure:

Filename: 

index.js 




const request = require('request');
let ACCESS_TOKEN = 'Your_API_KEY';
 
const reverseGeocoding = function (latitude, longitude) {
 
              + longitude + ', ' + latitude
              + '.json?access_token=' + ACCESS_TOKEN;
 
    request({ url: url, json: true }, function (error, response) {
        if (error) {
            console.log('Unable to connect to Geocode API');
        } else if (response.body.features.length == 0) {
            console.log('Unable to find location. Try to'
                + ' search another location.');
        } else {
            console.log(response.body.features[0].place_name);
        }
    })
}
 
// Sample data (Indore lat-long)
let latitude = 22.7196;
let longitude = 75.8577;
 
// Function call
reverseGeocoding(latitude, longitude);

Steps to run the program:

Make sure you have installed the request module using the following command:

npm install request

Run the index.js file using the below command:

node index.js

So this is how you can use the Mapbox Reverse Geocoding API which converts geographic coordinates into geographic features or features that exist at that location.

Article Tags :