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 an ArcGIS account to access the developer dashboard and create an API key.

Steps

Create a new pen

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

Set the API key

To access location services, you need an API key or OAuth 2.0 access token.

  1. Go to your dashboard to get an API key. The API key must be scoped to access the services used in this tutorial.

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

    Change line
              
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    //
    
    const apiKey = "YOUR_API_KEY";
    const basemapEnum = "ArcGIS:Streets";
    const map = new mapboxgl.Map({
      container: "map", // the id of the div element
      style: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${basemapEnum}?type=style&token=${apiKey}`,
      zoom: 12, // starting zoom
      center: [-118.805, 34.027] // starting location [longitude, latitude]
    });
    

Add references to ArcGIS REST JS

This tutorial uses ArcGIS REST JS for reverse geocoding.

  1. In the <head> element, add references to the ArcGIS REST JS library.
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
72
73
74
75
76
77
78
79
80
81
82
83
84
<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
    <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />

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

    <style>
      html,
      body,
      #map {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
        font-family: Arial, Helvetica, sans-serif;
        font-size: 14px;
        color: #323232;
    </style>

  </head>

  <body>
    <div id="map"></div>

  </body>

  <script>
    const apiKey = "YOUR_API_KEY";
    const basemapEnum = "ArcGIS:Navigation";
    const map = new mapboxgl.Map({
      container: "map",
      style: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${basemapEnum}?type=style&token=${apiKey}`,
      zoom: 12,
      center: [2.3522,48.8566] // Paris
    map.on("click", (e) => {
      const coords = e.lngLat.toArray();
      const authentication = new arcgisRest.ApiKey({
        key: apiKey
        .then((result) => {
          const lngLat = [result.location.x, result.location.y];
          const label = `${result.address.LongLabel}<br>${lngLat[0].toLocaleString()}, ${lngLat[1].toLocaleString()}`;
          new mapboxgl
        .catch((error) => {
          alert("There was a problem using the reverse geocode service. See the console for details.");
          console.error(error);
  </script>

</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
                                                                                        
    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
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
        <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />
    
        <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>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
      </body>
    
      <script>
    
        const apiKey = "YOUR_API_KEY";
    
        const basemapEnum = "ArcGIS:Navigation";
    
        const map = new mapboxgl.Map({
          container: "map",
          style: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${basemapEnum}?type=style&token=${apiKey}`,
          zoom: 12,
    
          center: [2.3522,48.8566] // Paris
    
        });
    
        map.on("click", (e) => {
          const coords = e.lngLat.toArray();
          const authentication = new arcgisRest.ApiKey({
            key: apiKey
            .then((result) => {
              const lngLat = [result.location.x, result.location.y];
              const label = `${result.address.LongLabel}<br>${lngLat[0].toLocaleString()}, ${lngLat[1].toLocaleString()}`;
              new mapboxgl
            .catch((error) => {
              alert("There was a problem using the reverse geocode service. See the console for details.");
              console.error(error);
      </script>
    
    </html>

Add a click event handler

Before you call the Geocoding service, you need to get a location from the user. You can add a handler to the Map's click event. The click handler will be called with an object containing parameters such as lngLat.

The lngLat property is a LngLat. It has lng and lat properties and a toArray method which returns an array in [longitude, latitude] order.

  1. After the map initialization code, add a handler for the click event. Create a coordinates array from the lngLat property of the event parameter.

    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
        <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />
    
        <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>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
      </body>
    
      <script>
    
        const apiKey = "YOUR_API_KEY";
    
        const basemapEnum = "ArcGIS:Navigation";
    
        const map = new mapboxgl.Map({
          container: "map",
          style: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${basemapEnum}?type=style&token=${apiKey}`,
          zoom: 12,
    
          center: [2.3522,48.8566] // Paris
    
        });
    
        map.on("click", (e) => {
          const coords = e.lngLat.toArray();
    
          const authentication = new arcgisRest.ApiKey({
            key: apiKey
            .then((result) => {
              const lngLat = [result.location.x, result.location.y];
              const label = `${result.address.LongLabel}<br>${lngLat[0].toLocaleString()}, ${lngLat[1].toLocaleString()}`;
              new mapboxgl
            .catch((error) => {
              alert("There was a problem using the reverse geocode service. See the console for details.");
              console.error(error);
        });
    
      </script>
    
    </html>

Call the 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.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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
        <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />
    
        <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>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
      </body>
    
      <script>
        const apiKey = "YOUR_API_KEY";
        const basemapEnum = "ArcGIS:Navigation";
        const map = new mapboxgl.Map({
          container: "map",
          style: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${basemapEnum}?type=style&token=${apiKey}`,
          zoom: 12,
          center: [2.3522,48.8566] // Paris
        map.on("click", (e) => {
          const coords = e.lngLat.toArray();
    
          const authentication = new arcgisRest.ApiKey({
            key: apiKey
          });
    
          arcgisRest
            .reverseGeocode(coords, {
              authentication
            })
            .then((result) => {
    
              const lngLat = [result.location.x, result.location.y];
              const label = `${result.address.LongLabel}<br>${lngLat[0].toLocaleString()}, ${lngLat[1].toLocaleString()}`;
              new mapboxgl
            .catch((error) => {
              alert("There was a problem using the reverse geocode service. See the console for details.");
              console.error(error);
        });
      </script>
    
    </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.

Use the results to display a pop-up with the address and location values.

  1. Convert the result.location object into an array of [longitude, latitude].

    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
        <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />
    
        <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>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
      </body>
    
      <script>
        const apiKey = "YOUR_API_KEY";
        const basemapEnum = "ArcGIS:Navigation";
        const map = new mapboxgl.Map({
          container: "map",
          style: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${basemapEnum}?type=style&token=${apiKey}`,
          zoom: 12,
          center: [2.3522,48.8566] // Paris
        map.on("click", (e) => {
          const coords = e.lngLat.toArray();
          const authentication = new arcgisRest.ApiKey({
            key: apiKey
          arcgisRest
            .reverseGeocode(coords, {
              authentication
            })
            .then((result) => {
    
              const lngLat = [result.location.x, result.location.y];
    
              const label = `${result.address.LongLabel}<br>${lngLat[0].toLocaleString()}, ${lngLat[1].toLocaleString()}`;
              new mapboxgl
            .catch((error) => {
              alert("There was a problem using the reverse geocode service. See the console for details.");
              console.error(error);
      </script>
    
    </html>
  2. Set a variable containing the text you want to display, using the address and coordinates.

    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
        <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />
    
        <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>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
      </body>
    
      <script>
        const apiKey = "YOUR_API_KEY";
        const basemapEnum = "ArcGIS:Navigation";
        const map = new mapboxgl.Map({
          container: "map",
          style: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${basemapEnum}?type=style&token=${apiKey}`,
          zoom: 12,
          center: [2.3522,48.8566] // Paris
        map.on("click", (e) => {
          const coords = e.lngLat.toArray();
          const authentication = new arcgisRest.ApiKey({
            key: apiKey
            .then((result) => {
              const lngLat = [result.location.x, result.location.y];
    
              const label = `${result.address.LongLabel}<br>${lngLat[0].toLocaleString()}, ${lngLat[1].toLocaleString()}`;
    
              new mapboxgl
              })
            .catch((error) => {
              alert("There was a problem using the reverse geocode service. See the console for details.");
              console.error(error);
      </script>
    
    </html>
  3. Add a pop-up to display the label at the returned coordinates.

    To add a pop-up to the map, you use the Popup class in Mapbox GL JS. See the Display a pop-up tutorial for more information.

    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
        <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />
    
        <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>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
      </body>
    
      <script>
        const apiKey = "YOUR_API_KEY";
        const basemapEnum = "ArcGIS:Navigation";
        const map = new mapboxgl.Map({
          container: "map",
          style: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${basemapEnum}?type=style&token=${apiKey}`,
          zoom: 12,
          center: [2.3522,48.8566] // Paris
        map.on("click", (e) => {
          const coords = e.lngLat.toArray();
          const authentication = new arcgisRest.ApiKey({
            key: apiKey
            .then((result) => {
              const lngLat = [result.location.x, result.location.y];
    
              const label = `${result.address.LongLabel}<br>${lngLat[0].toLocaleString()}, ${lngLat[1].toLocaleString()}`;
    
              new mapboxgl
                .Popup()
                .setLngLat(lngLat)
                .setHTML(label)
                .addTo(map);
    
              })
            .catch((error) => {
              alert("There was a problem using the reverse geocode service. See the console for details.");
              console.error(error);
      </script>
    
    </html>

Handle errors

To handle errors that occur when accessing the Geocoding service, such as networking or API key issues, use an error handler to show a message to the user.

  1. Add a handler to catch any exception and display a message to the user.

    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
        <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />
    
        <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>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
      </body>
    
      <script>
        const apiKey = "YOUR_API_KEY";
        const basemapEnum = "ArcGIS:Navigation";
        const map = new mapboxgl.Map({
          container: "map",
          style: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${basemapEnum}?type=style&token=${apiKey}`,
          zoom: 12,
          center: [2.3522,48.8566] // Paris
        map.on("click", (e) => {
          const coords = e.lngLat.toArray();
          const authentication = new arcgisRest.ApiKey({
            key: apiKey
          });
    
          arcgisRest
            .reverseGeocode(coords, {
              authentication
            })
            .then((result) => {
    
              const lngLat = [result.location.x, result.location.y];
    
              const label = `${result.address.LongLabel}<br>${lngLat[0].toLocaleString()}, ${lngLat[1].toLocaleString()}`;
    
              new mapboxgl
                .Popup()
                .setLngLat(lngLat)
                .setHTML(label)
                .addTo(map);
    
              })
    
            .catch((error) => {
              alert("There was a problem using the reverse geocode service. See the console for details.");
              console.error(error);
            });
      </script>
    
    </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:

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