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

In this tutorial, you use the locator 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.

    Use dark colors for code blocks
        
    Change line
    1
    2
    3
    4
    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 Maps SDK for JavaScript is available as AMD modules and ES modules, but this tutorial is based on AMD. The AMD require function uses references to determine which modules will be loaded – for example, you can specify "esri/Map" for loading the Map module. After the modules are loaded, they are passed as parameters (e.g. Map) to the callback function where they can be used in your application. It is important to keep the module references and callback parameters in the same order. For more information on the different types of modules, visit the Introduction to Tooling guide topic.

    Expand
    Use dark colors for code blocks
                                                                           
    Add line.Change line
    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
    require([
      "esri/config",
      "esri/Map",
      "esri/views/MapView",
    
      "esri/rest/locator"
    
    ], function(esriConfig, Map, MapView, locator) {
    
    Expand

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.

    Expand
    Use dark colors for code blocks
                                                                           
    Change line
    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
      esriConfig.apiKey = "YOUR_API_KEY";
    
      const map = new Map({
        basemap: "arcgis-navigation"
      });
    
    Expand
  2. Update the center property to -78.50169,-0.21489, and set the zoom property to 12.

    Expand
    Use dark colors for code blocks
                                                                           
    Change lineChange line
    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
      const view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-78.50169,-0.21489],
        zoom: 12
      });
    
    
    Expand

Define service url

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

  1. Define a variable, serviceUrl, to reference the Geocoding service.

    Expand
    Use dark colors for code blocks
                                                                           
    Add line.
    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
      const view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-78.50169,-0.21489],
        zoom: 12
      });
    
      const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    
    Expand

Reverse geocode

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.

    Expand
    Use dark colors for code blocks
                                                                           
    Add line.Add line.Add line.Add line.Add line.Add line.
    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
      const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    
      view.on("click", function(evt){
          const params = {
            location: evt.mapPoint
          };
    
        });
    
    Expand
  2. Create a showAddress function to display coordinates and the corresponding address.

    Expand
    Use dark colors for code blocks
                                                                           
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
      view.on("click", function(evt){
          const params = {
            location: 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
        });
      }
    
    Expand
  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.

    Expand
    Use dark colors for code blocks
                                                                           
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
      view.on("click", function(evt){
          const params = {
            location: evt.mapPoint
          };
    
         locator.locationToAddress(serviceUrl, 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);
            });
    
        });
    
    Expand

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:

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