Developers

Find address coordinates using Node.js

Do you want to learn how to build a simple Node.js application to find the location of an address? If so, keep reading to follow the step-by-step instructions below.

geocode({
  address: "1600 Pennsylvania Ave",
  countryCode: "USA"
}).then((res) => {
  console.log(res);
});

Prerequisites

To start building this application, you’ll need a few things:

  1. Account: An ArcGIS Developer or ArcGIS Online account. If you don’t have one, you can sign up for a free developer account. This account will give you access to services such as the basemap layer service, geocoding service, and routing service.
  2. Access token: Create an API key that is scoped to access the geocoding service.
  3. Node.js: An environment to run Node.js. We use Visual Studio Code below.

Install ArcGIS REST JS packages

The easiest way to find the location of an address is to use ArcGIS REST JS and the geocoding service. Use Visual Studio Code to set up a Node.js project. Next reference two ArcGIS REST packages. The first is the request package. This contains the authentication helper to validate your requests. The second is the geocoding package. This is a library you can use to access different geocoding operations such as findAddressCandidates, geocode, reverseGeocode, suggest, and geocodeAddresses (batch geocoding). To install each of these packages, use npm with the following commands:

npm install @esri/arcgis-rest-request @esri/arcgis-rest-geocoding

Create the script file

1. In Visual Studio Code, reference the components you need by specifying the exports from the ES modules.

import { ApiKeyManager } from '@esri/arcgis-rest-request'; 
import { geocode } from '@esri/arcgis-rest-geocoding';

2. Go to ArcGIS developer dashboard to get an API key. Ensure it is scoped (Geocoding (not stored)) to access the geocoding service.

API key in dashboard

3. In Visual Studio Code, define an authentication parameter for the request using the ApiKeyManager import.

API keys should be kept secure and should not be committed to a repository. It is recommended that you use an environment variable or external config file (ignored in .gitignore) for the API key.

const apiKey = "YOUR_API_KEY";
const authentication = ApiKeyManager.fromKey(apiKey);

4.  Access the geocoding service with the geocode import.

Use the geocode method with the following parameters to get address candidates with a latitude, longitude, and correct address. To get the most accurate results, use the addresspostalcountryCode, and authentication parameters, as shown below.

geocode({
  address: "N Main st",
  postal: 38103,
  countryCode: "United States",
  authentication,
});

5. Finally, before running the application, add code to log the results to the console.

The entire script file should look like this:

import { ApiKeyManager } from "@esri/arcgis-rest-request";
import { geocode } from "@esri/arcgis-rest-geocoding";
const apiKey = "YOUR_API_KEY";
const authentication = ApiKeyManager.fromKey(apiKey);
geocode({
  address: "N Main st",
  postal: 38103,
  countryCode: "USA",
  authentication,
}).then((response) => {
    response.candidates.forEach((res) => {
      console.log(res);
    })
});

Run the application

Run the application in the terminal window. The results should resemble the example below:

Output

The results include the address candidates that were found by the geocoding service. Each candidate contains the following:

Other values are returned such as the extent and spatial reference.

Additional resources

To further your learning, refer to the search for an address tutorial on the ArcGIS REST JS documentation page and the video below. You can also find the complete code solution in this repository.

Conclusion

This small Node.js application shows how you can use ArcGIS REST JS to perform address geocoding in just a few steps. ArcGIS REST JS is also an easy-to-use, lightweight library that can be utilized to perform many other operations such as routingdata queries, and demographic queries. So if you are building mapping solutions, be sure to check it out!

We are always working to provide developers with helpful resources, so let us know what you think! Tweet us at @ArcGISDevs or leave feedback on any of the guide pages at developers.arcgis.com.

About the author

Courtney Yatteau is a developer advocate on the Developer Experience team. She is responsible for outreach and awareness related to Esri's developer SDKs and APIs. Before Esri, Courtney worked in the secondary education field, teaching computer science and mathematics.

Connect:

Next Article

Using Arcade to Translate Pop-Ups for Use in the ArcGIS Instant Apps

Read this article