Node Js Geocoding to get Latitude and Longitude of Address Using Mapbox API - Node Js Tutorial

August 02, 2019

After writing an article about Simple Weather App Using Darksky API. In this tutorial, I will share with you guys how to use Geocoding in Node Js using Mapbox API service.

places api nodejs, free geocoding api, free geocoding api nodejs, Node Js Geocoding to get Latitude and Longitude, geocoding api for nodejs, places api nodejs, free geocoding api, free geocoding api nodejs, Node Js Geocoding to get Latitude and Longitude, geocoding api for nodejs
Node Js Geocoding to get Latitude and Longitude of Address Using Mapbox API - Node Js Tutorial

Node Js Geocoding to get Latitude and Longitude of Address Using Mapbox API - Node Js Tutorial

What is Geocoding ?

Geocoding is the process of converting addresses (like a street address, country, or any specific area) into geographic coordinates for example latitude and longitude.

In this tutorial, I will use Mapbox to get the Geocoding service. We have can use the Google Maps API which is way more familiar, why we use Mapbox ?

Mapbox API 

Mapbox is a data platform service for map and location (and it becomes a Google Maps alternative). It can definitely offer detailed, customizable, and interactive maps. Mapbox has many similar feature functionality like Google Maps. Mapbox can provide custom heatmaps, markers, marker clustering, and much more.

Moreover, many says that Mapbox is cheaper than Google Maps, easy to set up, and offers similar (and often better) functionality. Plus, we can make custom maps that can fit the look or design of of site or brand.  However, don't worry because we can also use Mapbox for free (of course with limitation).

How to install

1. Install request module
npm install request

2. Make Account in mapbox.com
Once done, go to this link https://docs.mapbox.com/api/search/#geocoding
you can get your complete link there, like the image below
places api nodejs, free geocoding api, free geocoding api nodejs, Node Js Geocoding to get Latitude and Longitude, geocoding api for nodejs, places api nodejs, free geocoding api, free geocoding api nodejs, Node Js Geocoding to get Latitude and Longitude, geocoding api for nodejs
mapbox documentation

Then you can copy the API url to your app

3. Create app.js and put these codes

//geocoding
const place = 'jakarta'
const mapboxGeocodeUrl = `https://api.mapbox.com/geocoding/v5/mapbox.places/${place}.json?limit=1&access_token=pk.eyJ1IjoiemVuaHV6YWluaSIsImEiOiJjanlzeXRobTQwMTZ3M2JwMXFsdXFlbDdsIn0.-MWjWymxxtz1_1BbMPmmRg`

request({ url: urlGeocoding, json: true }, (error, Response) => {
    if (error) { //this errorr will only show if there is no internet connection
        console.log('check your internet conection..')
    } else if (Response.body.features.length == 0) { // this error will be shown if our query search of an address, country or etc. is nowhere to be found
        console.log('cek your query to determine the area, we cannot return the information!')
    } else {
        const longitude = Response.body.features[0].center[0];
        const latitude = Response.body.features[0].center[1]
        console.log('longitude => ' + longitude)
        console.log('latitude => ' + latitude)
    }
})

4. run in the console
node app.js


you can check this project in my github https://github.com/ZenHuzaini/nodejs-geocoding


In the next tutorial I will try to combine the Geocoding result (latitude and longitude) from Mapbox and use it to get more dynamic weather information.

tag : places api nodejs, free geocoding api, free geocoding api nodejs, Node Js Geocoding to get Latitude and Longitude, geocoding api for nodejs, places api nodejs, free geocoding api, free geocoding api nodejs, Node Js Geocoding to get Latitude and Longitude, geocoding api for nodejs

Share this

Related Posts

Previous
Next Post »