Learn how to find an address near a

Reverse geocoding is the process of converting a 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
Prerequisites
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or
.
Get an access token
You need an
- Go to the Create an API key tutorial and create an
API key with the followingprivilege(s) :
- 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 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 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 reverseGeocode operation.
- Define a variable,
serviceUrl, to reference theGeocoding service .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
-
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 apop-up .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 calllocationToAddresstoreverse geocode themapPoint. Use theshowAddressfunction to display apop-up 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
What’s next?
Learn how to use additional SDK features and ArcGIS services in these tutorials: