Reverse geocode

Learn how to find an address near a with the .

Reverse geocode

Reverse geocoding is the process of converting a to an address or . To reverse geocode, use the 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 module.

In this tutorial, you will use the locator to and find the closest address to your clicked location on the map.

Prerequisites

Steps

Create a new pen

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

Get an access token

You need an with the correct privileges to access the used in this tutorial.

  1. Go to the Create an API key tutorial and create an with the following :
    • Privileges
      • Location services > Basemaps
      • Location services > Geocoding
  2. In CodePen, set esriConfig.apiKey to your access token.
    1
    2
    3
    4
    5
    6
    7
    8
    9
      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 is typically used in geocoding applications. Update the basemap attribute to use the arcgis/navigation basemap layer and change the position of the map to center on Quito.

  1. Update the basemap attribute from arcgis/topographic to arcgis/navigation. Then, update the center attribute to -78.50169,-0.21489, and set the zoom attribute to 12.
    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
        <arcgis-map basemap="arcgis/navigation" center="-78.50169, -0.21489" zoom="12">
    
          <arcgis-zoom position="top-left"></arcgis-zoom>
    
        </arcgis-map>
    
    Expand

Add modules and event listener

  1. Add a <script> tag in the <body> following the <arcgis-map> component with a require statement. In the require statement, add the locator module.

    Within the require statement, use the document.querySelector() method to access the map component.

    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
        <script>
    
          require(["esri/rest/locator"], (locator) => {
    
            const arcgisMap = document.querySelector("arcgis-map");
    
          });
    
        </script>
    
    Expand
  2. Create an event listener to listen for the map component's arcgisViewReadyChange event.

    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
        <script>
    
          require(["esri/rest/locator"], (locator) => {
    
            const arcgisMap = document.querySelector("arcgis-map");
    
            arcgisMap.addEventListener("arcgisViewReadyChange", () => {
    
            });
    
          });
    
        </script>
    
    Expand

Define service url

Use the locator module to access the and the reverseGeocode operation.

  1. Define a variable, serviceUrl, to reference the .
    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
            arcgisMap.addEventListener("arcgisViewReadyChange", () => {
    
              const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    
            });
    
    Expand

Reverse geocode

Use an event listener to capture a click location on the map and then call the . The service returns an address if an address is found, or an error if it cannot find a result. Display the results in a with the latitude, longitude, and address.

  1. In the arcgisViewReadyChange event listener, add an arcgisViewClick event handler to the map component. Create params and set the location to evt.detail.mapPoint.

    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
              const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    
              arcgisMap.addEventListener("arcgisViewClick", (evt) => {
                const params = {
                  location: evt.detail.mapPoint
                };
    
              });
    
    Expand
  2. Create a showAddress function to display coordinates and the corresponding address within a .

    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
              arcgisMap.addEventListener("arcgisViewClick", (evt) => {
                const params = {
                  location: evt.detail.mapPoint
                };
    
              });
    
              function showAddress(address, pt) {
                arcgisMap.openPopup({
                  title: +Math.round(pt.longitude * 100000) / 100000 + ", " + Math.round(pt.latitude * 100000) / 100000,
                  content: address,
                  location: pt
                });
              }
    
    Expand
  3. Update the arcgisViewClick handler to call locationToAddress to the mapPoint. Use the showAddress function to display a with the results.

    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
              arcgisMap.addEventListener("arcgisViewClick", (evt) => {
                const params = {
                  location: evt.detail.mapPoint
                };
    
                locator.locationToAddress(serviceUrl, params).then(
                  (response) => {
                    // Show the address found
                    const address = response.address;
                    showAddress(address, evt.detail.mapPoint);
                  },
                  (error) => {
                    // Show no address found
                    showAddress("No address found.", evt.detail.mapPoint);
                  }
                );
    
              });
    
    Expand

Run the App

In CodePen, run your code to display the map.

Click on the map to a and return a with the closest address.

What's next?

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

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.

The developer dashboard has moved

You can no longer sign into this site. Go to your ArcGIS portal or the ArcGIS Location Platform dashboard to perform management tasks.

Your ArcGIS portal

Create, manage, and access API keys and OAuth 2.0 developer credentials, hosted layers, and data services.

Your ArcGIS Location Platform dashboard

Manage billing, monitor service usage, and access additional resources.

Learn more about these changes in the What's new in Esri Developers June 2024 blog post.

Close