Learn how to find an address near a location
Reverse geocoding is the process of converting a locationreverse operation. This operation requires an initial location and returns an address with attributes such as place name and location.
In this tutorial, you use the reverse method of ArcGIS REST JS to reverse geocode and find the closest address to your clicked location on the map.
Prerequisites
You need an ArcGIS Location Platform or ArcGIS Online account.
Steps
Get the starter app
Select a type of authentication and follow the steps to create a new app.
Choose API key authentication if you:
- Want the easiest way to get started.
- Want to build public applications
A public application is an application that allows anonymous access without requiring users to sign in with an ArcGIS account. It supports API key or app authentication. that access ArcGIS Location ServicesArcGIS 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. and secure itemsAn item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. . - Have an ArcGIS Location Platform or ArcGIS Online account.
Choose user authentication if you:
- Want to build private applications.
- Require application users to sign in with their own ArcGIS account
An ArcGIS account is an identity with a user type and set of privileges that can access specific ArcGIS products, tools, APIs, services, and resources. The main account types that can be used for development are an ArcGIS Location Platform account, ArcGIS Online account, and ArcGIS Enterprise account. ArcGIS Location Platform and ArcGIS Online accounts are also associated with a subscription. and access resources their behalf. - Have an ArcGIS Online account.
To learn more about both types of authentication, go to Authentication.
Set up authentication
Set developer credentials
Use the API key or OAuth developer credentials
Add references
This tutorial uses ArcGIS REST JS for reverse geocoding, and the ol-popup library to display pop-ups.
-
In the
<headelement, add references to the ArcGIS REST JS and ol-popup libraries.> Use dark colors for code blocks <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@v10.9.0/ol.css"> <script src="https://cdn.jsdelivr.net/npm/ol@v10.9.0/dist/ol.js"></script> <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@13.4.1/dist/olms.js"></script> <script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script> <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@4/dist/bundled/geocoding.umd.js"></script> <script src="https://unpkg.com/ol-popup@5.1.1/dist/ol-popup.js"></script> <link rel="stylesheet" href="https://unpkg.com/ol-popup@5.1.1/src/ol-popup.css">
Update the map
An arcgis/navigation basemap style
-
Update the basemap and the map initialization to center on location
[2.3522,48.8566], Paris.Use dark colors for code blocks Copy <script> /* Use for API key authentication */ const accessToken = "YOUR_ACCESS_TOKEN"; // or /* Use for user authentication */ // const session = await arcgisRest.ArcGISIdentityManager.beginOAuth2({ // clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials // redirectUri: "YOUR_REDIRECT_URI", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const map = new ol.Map({ target: "map" }); const view = new ol.View({ center: ol.proj.fromLonLat([2.3522, 48.8566]), // Paris zoom: 12 }); map.setView(view); const basemapId = "arcgis/navigation"; olms.apply(map, `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapId}?token=${accessToken}`).then(() => { // Add Esri attribution // Learn more in https://esriurl.com/attribution const source = map.getLayers().item(0).getSource(); const poweredByEsriString = "Powered by <a href='https://www.esri.com/en-us/home' target='_blank'>Esri</a> | "; const attributionFn = source.getAttributions(); if (attributionFn) { source.setAttributions((ViewStateLayerStateExtent) => { return [poweredByEsriString, ...attributionFn(ViewStateLayerStateExtent)]; }); } else source.setAttributions(poweredByEsriString); }); </script>
Add a pop-up
Create a Popup to display the results of the reverse geocode. It is a type of Overlay so you add it to the map with map.add.
-
Create a
Popupand save it to apopupvariable. Add it to the map withmap.add.Overlay Use dark colors for code blocks const view = new ol.View({ center: ol.proj.fromLonLat([2.3522, 48.8566]), // Paris zoom: 12 }); map.setView(view); const popup = new Popup(); map.addOverlay(popup);
Add click event handler
Before you call the reverse Geocoding service, you need the location of the clicked point. Use ol.proj.transform to convert this into a latitude and longitude.
-
Add a click handler to the map. Inside, convert the clicked coordinates from the event object into latitude and longitude.
Use dark colors for code blocks const popup = new Popup(); map.addOverlay(popup); map.on("click", (e) => { const coords = ol.proj.transform(e.coordinate, "EPSG:3857", "EPSG:4326"); });
Call reverse geocode service
Use the ArcGIS REST JS reverse method to find an address closest to a point.
-
Inside the click handler, create a new
arcgisto access the Geocoding service. Call theRest. Api Key Manager arcgismethod with the coordinates array.Rest.reverse Geocode Use dark colors for code blocks const coords = ol.proj.transform(e.coordinate, "EPSG:3857", "EPSG:4326"); const authentication = arcgisRest.ApiKeyManager.fromKey(accessToken); arcgisRest .reverseGeocode(coords, { authentication })
Display the result
The response from the reverse operation contains two properties. address is a structured object with fields such as street name and business name. location contains the locationcatch handler to check for errors accessing the service.
-
Add a
thenhandler. Usepopup.showto display the resulting address and coordinates.Use dark colors for code blocks arcgisRest .reverseGeocode(coords, { authentication }) .then((result) => { const message = `${result.address.LongLabel}<br>` + `${result.location.x.toLocaleString()}, ${result.location.y.toLocaleString()}`; popup.show(e.coordinate, message); }) -
Add a
catchhandler. In most cases, errors occur when the user clicks in the water, so simply hide the pop-up when this happens. Usepopup.hide()to remove the pop-up and then write the error to the console.Use dark colors for code blocks popup.show(e.coordinate, message); }) .catch((error) => { popup.hide(); console.error(error); });
Run the app
Run the app.
Click on the map to reverse geocode the clicked point and display a pop-up with the closest address and coordinates.What's next?
Learn how to use additional location services in these tutorials:


