Getting Coordinates with node-geocoder

Michael Chen
1 min readSep 13, 2020

Geocoding is the process of taking input text, such as the address, or the name of that place, and returning the latitude and longitude for that place. With those coordinates, we can plot the location on the map. There are many libraries that does geocoding for us. A few reasons to use one these libraries include getting coordinates to visualize data on map, getting the address for a location, and reverse geocoding. Visualizing data on a map can even help companies make strategic decisions.

Node-geocoder is a library that has many different geocoder providers. A few notable ones include Mapquest, Google maps, Openstreenmap, Smartlystreet, and TomTom. You’re able to enter a string like “Petco 94134” and the geocoder will return a result that looks like:

latitude: 37.66748235longitude: -122.46673790018681country: "United States of America"city: "Daly City"state: "California"streetName: "PetSmart"countryCode: "US"provider: "openstreetmap"

To use Node-geocoder:

npm install node-geocoder to install the package, then

const options = {
provider: 'openstreetmap',

// Optional depending on the providers
fetch: customFetchImplementation,
apiKey: 'YOUR_API_KEY', // for Mapquest, OpenCage, Google Premier
formatter: null // 'gpx', 'string', ...
};

const geocoder = NodeGeocoder(options);

// Using callback
const res = await geocoder.geocode('<PLACE YOU WANT TO GEOCODE (example: golden gate bridge)>');
// output :
[
{
latitude: 37.8302731,
longitude: -122.4798443,
country: 'United States of America',
countryCode: 'US',
city: 'San Francisco',
zipcode: '94129,
streetName: 'Golden Gate Bridge',
provider: 'openstreetmap'
}
];

--

--