Learn how to find an address near a location

Reverse geocoding is the process of converting a locationreverseGeocode 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
Prerequisites
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or
.
Get an access token
You need an access token
- 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. 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. :
- Privileges
- Location services > Basemaps
- Location services > Geocoding
- Privileges
- In CodePen, set the
apiKeyproperty on the globalesriConfigvariable 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 layerbasemap attribute to use the arcgis/navigation basemap layer and change the position of the map to center on Quito.
- Update the
basemapattribute fromarcgis/topographictoarcgis/navigation. Then, update thecenterattribute to-78.50169,-0.21489, and set thezoomattribute to12.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
-
Add a
<script>tag in the<body>following the<arcgis-map>component. Use$arcgis.import()to add thelocatormodule.The ArcGIS Maps SDK for JavaScript
ArcGIS Maps SDK for JavaScript, previously known as ArcGIS API for JavaScript, is a developer product for building mapping and spatial analysis applications for the web. is available via CDN and npm, but this tutorial is based on CDN. The$arcgis.importglobal function accepts a module path or array of module paths, and returns a promise that resolves with the requested modules. This function can only be used when working with the CDN; otherwise, use the standard import syntax. To learn more about the SDK’s different modules, visit the References page.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> -
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 servicereverseGeocode operation.
- Define a variable,
serviceUrl, to reference the Geocoding serviceA 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. .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
-
Add an
arcgisViewClickevent handler to the map component. Createparamsand set thelocationtoevt.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> -
Create a
showAddressfunction to display coordinates and the corresponding address within a pop-upA pop-up is a visual element used to display data for features or graphics in a map. .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> -
Update the
arcgisViewClickhandler to calllocationToAddressto reverse geocodeReverse geocoding is the process of converting a point to its nearest address or place. themapPoint. Use theshowAddressfunction to display a pop-upA pop-up is a visual element used to display data for features or graphics in a map. 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 foundconst address = response.address;showAddress(address, evt.detail.mapPoint);},(error) => {// Show no address foundshowAddress("No address found.", evt.detail.mapPoint);},);});18 collapsed linesfunction 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
What’s next?
Learn how to use additional SDK features and ArcGIS services in these tutorials: