Learn how to find an address near a location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more with the Geocoding service A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. Learn more .

Reverse geocode

Reverse geocoding is the process of converting a location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more to an address or place A place, also known as a point of interest (POI), is a location that represents a business, administrative entity, or geographic feature around the world. A place can also have attributes associated with it, such as a name, address, category, and ID. Learn more . To reverse geocode, use the Geocoding service A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. Learn more and the reverseGeocode operation. This operation requires an initial location and returns an address with attributes such as place name and location. To simplify accessing the Geocoding service, you use the locator module.

In this tutorial, you will use the locator to reverse geocode Reverse geocoding is the process of converting a point to its nearest address or place. Learn more and find the closest address to your clicked location on the map.

Prerequisites

Steps

Create a new pen

  1. To get started, either complete the Display a map tutorial or .

Get an access token

You need an access token An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. Learn more with the correct privileges to access the location services ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more used in this tutorial.

  1. Go to the Create an API key tutorial and create an API key An API key is a long-lived access token created using API key credentials. They are valid for up to one year and are typically embedded directly into client applications. Learn more with the following privilege(s) Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more :
    • Privileges
      • Location services > Basemaps
      • Location services > Geocoding
  2. In CodePen, set the apiKey property on the global esriConfig variable to your access token.
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };

To learn about other ways to get an access token, go to Types of authentication.

Update the map

A streets basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more is typically used in geocoding applications. Update the basemap attribute to use the arcgis/navigation basemap layer and change the position of the map to center on Quito.

  1. Update the basemap attribute from arcgis/topographic to arcgis/navigation. Then, update the center attribute to -78.50169,-0.21489, and set the zoom attribute to 12.
    29 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Reverse geocode</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="-78.50169, -0.21489" zoom="12">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    5 collapsed lines
    </body>
    </html>

Add modules and reference map component

  1. Add a <script> tag in the <body> following the <arcgis-map> component. Use $arcgis.import() to add the locator module.

    26 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Reverse geocode</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="-78.50169, -0.21489" zoom="12">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
    </script>
    </body>
    3 collapsed lines
    </html>
  2. Use document.querySelector() to get a reference to the Map component, then wait for it to be ready with the viewOnReady method.

    35 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Reverse geocode</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="-78.50169, -0.21489" zoom="12">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    </script>
    5 collapsed lines
    </body>
    </html>

Define service url

Use the locator module to access the Geocoding service A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. Learn more and the reverseGeocode operation.

  1. Define a variable, serviceUrl, to reference the Geocoding service A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. Learn more .
    41 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Reverse geocode</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="-78.50169, -0.21489" zoom="12">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    6 collapsed lines
    </script>
    </body>
    </html>

Reverse geocode

Use an event listener to capture a click location on the map and then call the Geocoding service A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. Learn more . The service returns an address if an address is found, or an error if it cannot find a result. Display the results in a pop-up A pop-up is a visual element used to display data for features or graphics in a map. Learn more with the latitude, longitude, and address.

  1. Add an arcgisViewClick event handler to the map component. Create params and set the location to evt.detail.mapPoint.

    41 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Reverse geocode</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="-78.50169, -0.21489" zoom="12">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    viewElement.addEventListener("arcgisViewClick", (evt) => {
    const params = {
    location: evt.detail.mapPoint,
    };
    });
    7 collapsed lines
    </script>
    </body>
    </html>
  2. Create a showAddress function to display coordinates and the corresponding address within a pop-up A pop-up is a visual element used to display data for features or graphics in a map. Learn more .

    43 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Reverse geocode</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="-78.50169, -0.21489" zoom="12">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    viewElement.addEventListener("arcgisViewClick", (evt) => {
    const params = {
    location: evt.detail.mapPoint,
    };
    });
    function showAddress(address, pt) {
    viewElement.openPopup({
    title:
    +Math.round(pt.longitude * 100000) / 100000 +
    ", " +
    Math.round(pt.latitude * 100000) / 100000,
    content: address,
    location: pt,
    });
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  3. Update the arcgisViewClick handler to call locationToAddress to reverse geocode Reverse geocoding is the process of converting a point to its nearest address or place. Learn more the mapPoint. Use the showAddress function to display a pop-up A pop-up is a visual element used to display data for features or graphics in a map. Learn more with the results.

    43 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Reverse geocode</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="-78.50169, -0.21489" zoom="12">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    viewElement.addEventListener("arcgisViewClick", (evt) => {
    const params = {
    location: evt.detail.mapPoint,
    };
    locator.locationToAddress(serviceUrl, params).then(
    (response) => {
    // Show the address found
    const address = response.address;
    showAddress(address, evt.detail.mapPoint);
    },
    (error) => {
    // Show no address found
    showAddress("No address found.", evt.detail.mapPoint);
    },
    );
    });
    18 collapsed lines
    function showAddress(address, pt) {
    viewElement.openPopup({
    title:
    +Math.round(pt.longitude * 100000) / 100000 +
    ", " +
    Math.round(pt.latitude * 100000) / 100000,
    content: address,
    location: pt,
    });
    }
    </script>
    </body>
    </html>

Run the App

In CodePen, run your code to display the map.

Click on the map to reverse geocode Reverse geocoding is the process of converting a point to its nearest address or place. Learn more a location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more and return a pop-up A pop-up is a visual element used to display data for features or graphics in a map. Learn more with the closest address.

What’s next?

Learn how to use additional SDK features and ArcGIS services in these tutorials: