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. To simplify accessing the Geocoding service, you use the Locator task.

In this tutorial, you use the Locator task 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 services, you need an API key.

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

  2. In CodePen, set the apiKey to your key, so it can be used to access basemap layer and location services.

    Change line
        
    esriConfig.apiKey = "YOUR-API-KEY";
    const map = new Map({
      basemap: "arcgis-topographic" //Basemap layer service
    });
    

Add modules

  1. In the require statement, add the Locator module.

    The ArcGIS API for JavaScript uses AMD modules. The require function is used to load modules so they can be used in the main function. It's important to keep the module references and function parameters in the same order.

    Add line.Change line
                                                                              
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>ArcGIS API for JavaScript Tutorials: Reverse geocode</title>
    <style>
      html, body, #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19/"></script>
      <script>
    
    require([
      "esri/config",
      "esri/Map",
      "esri/views/MapView",
    
      "esri/tasks/Locator"
    
    ], function(esriConfig,Map, MapView, Locator) {
    
      esriConfig.apiKey = "YOUR-API-KEY";
    
      const map = new Map({
        basemap: "arcgis-navigation"
      });
    
      const view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-78.50169,-0.21489],
        zoom: 12
      });
    
      // Find address
      const locatorTask = new Locator ({
        url: "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"
      })
    
      view.on("click", function(evt){
          const params = {
            location: evt.mapPoint
          };
    
         locatorTask.locationToAddress(params)
            .then(function(response) { // Show the address found
              const address = response.address;
              showAddress(address, evt.mapPoint);
            }, function(err) { // Show no address found
              showAddress("No address found.", evt.mapPoint);
            });
    
        });
    
      function showAddress(address, pt) {
        view.popup.open({
          title:  + Math.round(pt.longitude * 100000)/100000 + ", " + Math.round(pt.latitude * 100000)/100000,
          content: address,
          location: pt
        });
      }
    
    });
    </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

Update the map

A streets basemap layer is typically used in geocoding applications. Update the basemap property to use the arcgis-navigation basemap layer and change the position of the map to center on Quito.

  1. Update the basemap property from arcgis-topographic to arcgis-navigation.

    Change line
                                                                              
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>ArcGIS API for JavaScript Tutorials: Reverse geocode</title>
    <style>
      html, body, #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19/"></script>
      <script>
    
    require([
      "esri/config",
      "esri/Map",
      "esri/views/MapView",
    
      "esri/tasks/Locator"
    
    ], function(esriConfig,Map, MapView, Locator) {
    
      esriConfig.apiKey = "YOUR-API-KEY";
    
      const map = new Map({
        basemap: "arcgis-navigation"
      });
    
      const view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-78.50169,-0.21489],
        zoom: 12
      });
    
      // Find address
      const locatorTask = new Locator ({
        url: "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"
      })
    
      view.on("click", function(evt){
          const params = {
            location: evt.mapPoint
          };
    
         locatorTask.locationToAddress(params)
            .then(function(response) { // Show the address found
              const address = response.address;
              showAddress(address, evt.mapPoint);
            }, function(err) { // Show no address found
              showAddress("No address found.", evt.mapPoint);
            });
    
        });
    
      function showAddress(address, pt) {
        view.popup.open({
          title:  + Math.round(pt.longitude * 100000)/100000 + ", " + Math.round(pt.latitude * 100000)/100000,
          content: address,
          location: pt
        });
      }
    
    });
    </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  2. Update the center property to -78.50169,-0.21489, and set the zoom property to 12.

    Change lineChange line
                                                                              
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>ArcGIS API for JavaScript Tutorials: Reverse geocode</title>
    <style>
      html, body, #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19/"></script>
      <script>
    
    require([
      "esri/config",
      "esri/Map",
      "esri/views/MapView",
    
      "esri/tasks/Locator"
    
    ], function(esriConfig,Map, MapView, Locator) {
    
      esriConfig.apiKey = "YOUR-API-KEY";
    
      const map = new Map({
        basemap: "arcgis-navigation"
      });
    
      const view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-78.50169,-0.21489],
        zoom: 12
      });
    
      // Find address
      const locatorTask = new Locator ({
        url: "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"
      })
    
      view.on("click", function(evt){
          const params = {
            location: evt.mapPoint
          };
    
         locatorTask.locationToAddress(params)
            .then(function(response) { // Show the address found
              const address = response.address;
              showAddress(address, evt.mapPoint);
            }, function(err) { // Show no address found
              showAddress("No address found.", evt.mapPoint);
            });
    
        });
    
      function showAddress(address, pt) {
        view.popup.open({
          title:  + Math.round(pt.longitude * 100000)/100000 + ", " + Math.round(pt.latitude * 100000)/100000,
          content: address,
          location: pt
        });
      }
    
    });
    </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

Create a locator task

A task makes a request to a service and returns the results. Use the Locator task to access the Geocoding service and the reverseGeocode operation.

  1. Create a Locator and set the url property to reference the Geocoding service.

    Add line.Add line.Add line.Add line.
                                                                              
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>ArcGIS API for JavaScript Tutorials: Reverse geocode</title>
    <style>
      html, body, #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19/"></script>
      <script>
    
    require([
      "esri/config",
      "esri/Map",
      "esri/views/MapView",
    
      "esri/tasks/Locator"
    
    ], function(esriConfig,Map, MapView, Locator) {
    
      esriConfig.apiKey = "YOUR-API-KEY";
    
      const map = new Map({
        basemap: "arcgis-navigation"
      });
    
      const view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-78.50169,-0.21489],
        zoom: 12
      });
    
      // Find address
      const locatorTask = new Locator ({
        url: "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"
      })
    
      view.on("click", function(evt){
          const params = {
            location: evt.mapPoint
          };
    
         locatorTask.locationToAddress(params)
            .then(function(response) { // Show the address found
              const address = response.address;
              showAddress(address, evt.mapPoint);
            }, function(err) { // Show no address found
              showAddress("No address found.", evt.mapPoint);
            });
    
        });
    
      function showAddress(address, pt) {
        view.popup.open({
          title:  + Math.round(pt.longitude * 100000)/100000 + ", " + Math.round(pt.latitude * 100000)/100000,
          content: address,
          location: pt
        });
      }
    
    });
    </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

Execute the task

Use a handler to capture a click location on the map and then call the Geocoding service. 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 with the latitude, longitude, and address.

  1. In the main function, add a click handler to the view. Create params and set the location to evt.mapPoint.

    Add line.Add line.Add line.Add line.Add line.Add line.
                                                                              
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>ArcGIS API for JavaScript Tutorials: Reverse geocode</title>
    <style>
      html, body, #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19/"></script>
      <script>
    
    require([
      "esri/config",
      "esri/Map",
      "esri/views/MapView",
    
      "esri/tasks/Locator"
    
    ], function(esriConfig,Map, MapView, Locator) {
    
      esriConfig.apiKey = "YOUR-API-KEY";
    
      const map = new Map({
        basemap: "arcgis-navigation"
      });
    
      const view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-78.50169,-0.21489],
        zoom: 12
      });
    
      // Find address
      const locatorTask = new Locator ({
        url: "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"
      })
    
      view.on("click", function(evt){
          const params = {
            location: evt.mapPoint
          };
    
         locatorTask.locationToAddress(params)
            .then(function(response) { // Show the address found
              const address = response.address;
              showAddress(address, evt.mapPoint);
            }, function(err) { // Show no address found
              showAddress("No address found.", evt.mapPoint);
            });
    
        });
    
      function showAddress(address, pt) {
        view.popup.open({
          title:  + Math.round(pt.longitude * 100000)/100000 + ", " + Math.round(pt.latitude * 100000)/100000,
          content: address,
          location: pt
        });
      }
    
    });
    </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  2. Create a showAddress function to display coordinates and the corresponding address.

    Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                              
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>ArcGIS API for JavaScript Tutorials: Reverse geocode</title>
    <style>
      html, body, #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19/"></script>
      <script>
    
    require([
      "esri/config",
      "esri/Map",
      "esri/views/MapView",
    
      "esri/tasks/Locator"
    
    ], function(esriConfig,Map, MapView, Locator) {
    
      esriConfig.apiKey = "YOUR-API-KEY";
    
      const map = new Map({
        basemap: "arcgis-navigation"
      });
    
      const view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-78.50169,-0.21489],
        zoom: 12
      });
    
      // Find address
      const locatorTask = new Locator ({
        url: "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"
      })
    
      view.on("click", function(evt){
          const params = {
            location: evt.mapPoint
          };
    
         locatorTask.locationToAddress(params)
            .then(function(response) { // Show the address found
              const address = response.address;
              showAddress(address, evt.mapPoint);
            }, function(err) { // Show no address found
              showAddress("No address found.", evt.mapPoint);
            });
    
        });
    
      function showAddress(address, pt) {
        view.popup.open({
          title:  + Math.round(pt.longitude * 100000)/100000 + ", " + Math.round(pt.latitude * 100000)/100000,
          content: address,
          location: pt
        });
      }
    
    });
    </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  3. Update the click handler to call locationToAddress to reverse geocode the mapPoint. Use the showAddress function to display a pop-up with the results.

    Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                              
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>ArcGIS API for JavaScript Tutorials: Reverse geocode</title>
    <style>
      html, body, #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19/"></script>
      <script>
    
    require([
      "esri/config",
      "esri/Map",
      "esri/views/MapView",
    
      "esri/tasks/Locator"
    
    ], function(esriConfig,Map, MapView, Locator) {
    
      esriConfig.apiKey = "YOUR-API-KEY";
    
      const map = new Map({
        basemap: "arcgis-navigation"
      });
    
      const view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-78.50169,-0.21489],
        zoom: 12
      });
    
      // Find address
      const locatorTask = new Locator ({
        url: "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"
      })
    
      view.on("click", function(evt){
          const params = {
            location: evt.mapPoint
          };
    
         locatorTask.locationToAddress(params)
            .then(function(response) { // Show the address found
              const address = response.address;
              showAddress(address, evt.mapPoint);
            }, function(err) { // Show no address found
              showAddress("No address found.", evt.mapPoint);
            });
    
        });
    
      function showAddress(address, pt) {
        view.popup.open({
          title:  + Math.round(pt.longitude * 100000)/100000 + ", " + Math.round(pt.latitude * 100000)/100000,
          content: address,
          location: pt
        });
      }
    
    });
    </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

Run the App

In CodePen, run your code to display the map.

Click on the map to reverse geocode a location and return a pop-up with the closest address.

What's next?

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