Reverse geocode

Learn how to find an address near a location with the geocoding service.

Reverse geocoding is the process of converting a location to an address or place. To reverse geocode, you use the Geocoding service and the reverseGeocode 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 reverseGeocode method of ArcGIS REST JS to reverse geocode and find the closest address to your clicked location on the map.

Prerequisites

You need a free ArcGIS developer account to access your dashboard and API keys. The API key must be scoped to access the services used in this tutorial.

Steps

Create a new pen

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

Set the API key

To access ArcGIS location services, you need an API key.

  1. Go to your dashboard to get an API key.

  2. In CodePen, update apiKey to use your key.

    Change line
        
    const apiKey = "YOUR-API-KEY";
    const basemapId = "ArcGIS:Streets";
    const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey;
    olms(map, basemapURL);
    

Add references

This tutorial uses ArcGIS REST JS for reverse geocoding, and the ol-popup library to display pop-ups.

  1. In the <head> element, add references to the ArcGIS REST JS and ol-popup libraries.

    Add line.Add line.Add line.Add line.Add line.
                                                                                                
    
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Reverse Geocode</title>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
          }
        </style>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <script>
    
          const apiKey = "YOUR-API-KEY";
    
          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 popup = new Popup();
          map.addOverlay(popup);
    
          map.on("click", (e) => {
    
            const coords = ol.proj.transform(e.coordinate, "EPSG:3857", "EPSG:4326");
    
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            });
    
            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);
              })
    
              .catch((error) => {
                popup.hide();
                console.error(error);
              });
    
          });
    
          const basemapId = "ArcGIS:Navigation";
    
          olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
    
        </script>
    
      </body>
    
    </html>

Update the map

A navigation basemap layer is typically used in geocoding and routing applications. Update the basemap layer to use ArcGIS:Navigation.

  1. Update the basemap and the map initialization to center on location [2.3522,48.8566], Paris.

    Change lineChange line
                                                                                                
    
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Reverse Geocode</title>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
          }
        </style>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <script>
    
          const apiKey = "YOUR-API-KEY";
    
          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 popup = new Popup();
          map.addOverlay(popup);
    
          map.on("click", (e) => {
    
            const coords = ol.proj.transform(e.coordinate, "EPSG:3857", "EPSG:4326");
    
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            });
    
            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);
              })
    
              .catch((error) => {
                popup.hide();
                console.error(error);
              });
    
          });
    
          const basemapId = "ArcGIS:Navigation";
    
          olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
    
        </script>
    
      </body>
    
    </html>

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.addOverlay.

  1. Create a Popup and save it to a popup variable. Add it to the map with map.addOverlay.

    Add line.Add line.
                                                                                                
    
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Reverse Geocode</title>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
          }
        </style>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <script>
    
          const apiKey = "YOUR-API-KEY";
    
          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 popup = new Popup();
          map.addOverlay(popup);
    
          map.on("click", (e) => {
    
            const coords = ol.proj.transform(e.coordinate, "EPSG:3857", "EPSG:4326");
    
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            });
    
            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);
              })
    
              .catch((error) => {
                popup.hide();
                console.error(error);
              });
    
          });
    
          const basemapId = "ArcGIS:Navigation";
    
          olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
    
        </script>
    
      </body>
    
    </html>

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.

  1. Add a click handler to the map. Inside, convert the clicked coordinates from the event object into latitude and longitude.

    Add line.Add line.Add line.Add line.Add line.
                                                                                                
    
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Reverse Geocode</title>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
          }
        </style>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <script>
    
          const apiKey = "YOUR-API-KEY";
    
          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 popup = new Popup();
          map.addOverlay(popup);
    
          map.on("click", (e) => {
    
            const coords = ol.proj.transform(e.coordinate, "EPSG:3857", "EPSG:4326");
    
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            });
    
            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);
              })
    
              .catch((error) => {
                popup.hide();
                console.error(error);
              });
    
          });
    
          const basemapId = "ArcGIS:Navigation";
    
          olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
    
        </script>
    
      </body>
    
    </html>

Call reverse geocode service

Use the ArcGIS REST JS reverseGeocode method to find an address closest to a point.

  1. Inside the click handler, create a new arcgisRest.ApiKey to access the geocoding service. Call the arcgisRest.reverseGeocode method with the coordinates array.

    Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                                
    
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Reverse Geocode</title>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
          }
        </style>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <script>
    
          const apiKey = "YOUR-API-KEY";
    
          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 popup = new Popup();
          map.addOverlay(popup);
    
          map.on("click", (e) => {
    
            const coords = ol.proj.transform(e.coordinate, "EPSG:3857", "EPSG:4326");
    
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            });
    
            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);
              })
    
              .catch((error) => {
                popup.hide();
                console.error(error);
              });
    
          });
    
          const basemapId = "ArcGIS:Navigation";
    
          olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
    
        </script>
    
      </body>
    
    </html>

Display the result

The response from the reverseGeocode operation contains two properties. address is a structured object with fields such as street name and business name. location contains the location of the returned address, which may differ from the coordinates you provided. Display these values in a pop-up. Use a catch handler to check for errors accessing the service.

  1. Add a then handler. Use popup.show to display the resulting address and coordinates.

    Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                                
    
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Reverse Geocode</title>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
          }
        </style>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <script>
    
          const apiKey = "YOUR-API-KEY";
    
          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 popup = new Popup();
          map.addOverlay(popup);
    
          map.on("click", (e) => {
    
            const coords = ol.proj.transform(e.coordinate, "EPSG:3857", "EPSG:4326");
    
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            });
    
            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);
              })
    
              .catch((error) => {
                popup.hide();
                console.error(error);
              });
    
          });
    
          const basemapId = "ArcGIS:Navigation";
    
          olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
    
        </script>
    
      </body>
    
    </html>
  2. Add a catch handler. In most cases, errors occur when the user clicks in the water, so simply hide the pop-up when this happens. Use popup.hide() to remove the pop-up and then write the error to the console.

    Add line.Add line.Add line.Add line.
                                                                                                
    
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Reverse Geocode</title>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
          }
        </style>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <script>
    
          const apiKey = "YOUR-API-KEY";
    
          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 popup = new Popup();
          map.addOverlay(popup);
    
          map.on("click", (e) => {
    
            const coords = ol.proj.transform(e.coordinate, "EPSG:3857", "EPSG:4326");
    
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            });
    
            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);
              })
    
              .catch((error) => {
                popup.hide();
                console.error(error);
              });
    
          });
    
          const basemapId = "ArcGIS:Navigation";
    
          olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
    
        </script>
    
      </body>
    
    </html>

Run the app

In CodePen, run your code to display the map.

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 ArcGIS location services in these tutorials: