Sometimes, we want to get city name from a latitude and longitude point with Node.js.
In this article, we’ll look at how to get city name from a latitude and longitude point with Node.js.
How to get city name from a latitude and longitude point with Node.js?
To get city name from a latitude and longitude point with Node.js, we can use the node-geocoder
package.
To install it, we run
npm i node-geocder
Then we use it by writing
const NodeGeocoder = require('node-geocoder');
const options = {
provider: 'google',
httpAdapter: 'https',
apiKey: '...',
formatter: 'json'
};
const geocoder = NodeGeocoder(options);
geocoder.reverse({
lat: 49.5967439,
lon: 77.3285038
}, (err, res) => {
console.log(res);
});
We create a NodeGeocoder
instance with some options
to specify the map provider, API key, etc, and the format of the data with the formatter
property.
Then we call geocoder.reverse
with an object that has the lat
latitude and lon
longitude.
The callback’s res
parameter has the location data for the given coordinates.
Conclusion
To get city name from a latitude and longitude point with Node.js, we can use the node-geocoder
package.