A CLI tool to get geographical coordinates from an address
Hero Geo Location CLI is a simple command-line tool to get geographical coordinates (latitude and longitude) from an address, get an address from coordinates, or search for locations using the Nominatim OpenStreetMap API.
To install the package globally, run:
npm install -g hero-geo-location
To use the package in your JavaScript project, run:
npm install hero-geo-location
To get the geographical coordinates of an address, use the following command:
geo "country,city,street,zipcode"
To get the address from geographical coordinates, use the following command:
geo <lat> <lon>
To search for locations using a query, use the following command:
geo search "query"
To get the geographical coordinates of an address in your JavaScript code, use the following code:
const {
getGeoLocation,
getReverseGeoLocation,
searchLocation,
} = require("hero-geo-location");
// Forward geocoding
getGeoLocation("country,city,street,zipcode")
.then((result) => {
console.log(result);
})
.catch((error) => {
console.error(error);
});
// Reverse geocoding
getReverseGeoLocation(lat, lon)
.then((result) => {
console.log(result);
})
.catch((error) => {
console.error(error);
});
// Search locations
searchLocation("query")
.then((results) => {
console.log(results);
})
.catch((error) => {
console.error(error);
});
geo "USA, New York, 5th Avenue, 10001"
Output:
{
"latitude": "40.7127281",
"longitude": "-74.0060152",
"address": "5th Avenue, New York, NY 10001, USA",
"type": "road",
"name": "5th Avenue, New York, NY 10001, USA",
"country": "United States",
"city": "New York",
"country_code": "us",
"boundingbox": ["40.7127281", "40.7127281", "-74.0060152", "-74.0060152"]
}
geo -34.44076 -58.70521
Output:
{
"latitude": "-34.44076",
"longitude": "-58.70521",
"address": "Some address in Buenos Aires, Argentina",
"type": "place",
"name": "Some address in Buenos Aires, Argentina",
"country": "Argentina",
"city": "Buenos Aires",
"country_code": "ar",
"boundingbox": ["-34.44076", "-34.44076", "-58.70521", "-58.70521"]
}
geo search "rewe Bahnhofstrae berlin"
Output:
[
{
"latitude": "52.4586902",
"longitude": "13.5772772",
"address": "REWE, 33-38, Bahnhofstrae, Dammvorstadt, Kpenick, Treptow-Kpenick, Berlin, 12555, Deutschland",
"type": "supermarket",
"name": "REWE, 33-38, Bahnhofstrae, Dammvorstadt, Kpenick, Treptow-Kpenick, Berlin, 12555, Deutschland",
"country": "Deutschland",
"city": "Berlin",
"country_code": "de",
"boundingbox": ["52.4585902", "52.4587902", "13.5771772", "13.5773772"]
},
{
"latitude": "52.4577394",
"longitude": "13.5789184",
"address": "REWE City, 23-25, Bahnhofstrae, Dammvorstadt, Kpenick, Treptow-Kpenick, Berlin, 12555, Deutschland",
"type": "supermarket",
"name": "REWE City, 23-25, Bahnhofstrae, Dammvorstadt, Kpenick, Treptow-Kpenick, Berlin, 12555, Deutschland",
"country": "Deutschland",
"city": "Berlin",
"country_code": "de",
"boundingbox": ["52.4576394", "52.4578394", "13.5788184", "13.5790184"]
}
]
const {
getGeoLocation,
getReverseGeoLocation,
searchLocation,
} = require("hero-geo-location");
// Forward geocoding
getGeoLocation("Germany, Berlin, Unter den Linden, 10117")
.then((result) => {
console.log(result);
})
.catch((error) => {
console.error(error);
});
// Reverse geocoding
getReverseGeoLocation(-34.44076, -58.70521)
.then((result) => {
console.log(result);
})
.catch((error) => {
console.error(error);
});
// Search locations
searchLocation("Unter den Linden 1 Berlin")
.then((results) => {
console.log(results);
})
.catch((error) => {
console.error(error);
});
Milad Davoodabadi
ISC