Find places

Learn how to search for coffee shops, gas stations, restaurants and other nearby places with the geocoding service.

Place finding is the process of searching for a place name or POI to find its address and location. You can use the geocoding service to find places such as coffee shops, gas stations, or restaurants for any geographic location around the world. You can search for places by name or by using categories. You can search near a location or you can search globally.

In this tutorial, you use ArcGIS REST JS to access the geocoding service and find places by place category.

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

This tutorial uses ArcGIS REST JS for place finding.

  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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!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;
      }
      .places {
        position: absolute;
        top: 20px;
        left: 20px;
      }
    </style>

  </head>

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

    <select id="places-select" style="left: 20px; top: 20px; position: absolute; font-size: 16px; padding:4px 8px;">
      <option value="">Choose a category...</option>
      <option value="Coffee shop">Coffee shops</option>
      <option value="Gas station">Gas stations</option>
      <option value="Food">Food</option>
      <option value="Hotel">Hotels</option>
      <option value="Parks and Outdoors">Parks and Outdoors</option>
    </select>

    <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: 13,
        center: [-122.4194,37.7749] // San Francisco
      map.once("load", () => {
        map.addSource("places", {
          type: "geojson",
          data: {
            type: "FeatureCollection",
            features: []
          id: "places-circle",
          source: "places",
          type: "circle",
          paint: {
            "circle-color": "hsla(200, 80%, 80%, 0.7)",
            "circle-stroke-color": "hsl(200, 80%, 40%)",
            "circle-stroke-width": 1,
            "circle-radius": 3
          id: "places-text",
          source: "places",
          type: "symbol",
          layout: {
            "text-field": ["get", "PlaceName"],
            "text-font": ["Arial Bold"],
            "text-variable-anchor": ["left","right"],
            "text-justify": "left",
            "text-radial-offset": 0.5
          paint: {
            "text-color": "hsl(200, 80%,40%)",
            "text-halo-color": "white",
            "text-halo-width": 2
      function showPlaces() {
        const authentication = new arcgisRest.ApiKey({
          key: apiKey
        const category = document.getElementById("places-select").value;
        if (!category) {
          return;
            outFields: "Place_addr,PlaceName",
            params: {
              location: map.getCenter().toArray().join(","),
              maxLocations: 25
          .then((response) => {
            map.getSource("places").setData(response.geoJson);
          .catch((error) => {
            alert("There was a problem using the geocoder. See the console for details.");
            console.error(error);
      document.getElementById("places-select").addEventListener("change", showPlaces);
    </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 [-122.4194,37.7749], San Francisco.

                                                                                                                                                   
    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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    <!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;
          .places {
            position: absolute;
            top: 20px;
            left: 20px;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <select id="places-select" style="left: 20px; top: 20px; position: absolute; font-size: 16px; padding:4px 8px;">
          <option value="">Choose a category...</option>
          <option value="Coffee shop">Coffee shops</option>
          <option value="Gas station">Gas stations</option>
          <option value="Food">Food</option>
          <option value="Hotel">Hotels</option>
          <option value="Parks and Outdoors">Parks and Outdoors</option>
        </select>
    
        <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: 13,
    
            center: [-122.4194,37.7749] // San Francisco
    
          });
          map.once("load", () => {
            map.addSource("places", {
              type: "geojson",
              data: {
                type: "FeatureCollection",
                features: []
              id: "places-circle",
              source: "places",
              type: "circle",
              paint: {
                "circle-color": "hsla(200, 80%, 80%, 0.7)",
                "circle-stroke-color": "hsl(200, 80%, 40%)",
                "circle-stroke-width": 1,
                "circle-radius": 3
              id: "places-text",
              source: "places",
              type: "symbol",
              layout: {
                "text-field": ["get", "PlaceName"],
                "text-font": ["Arial Bold"],
                "text-variable-anchor": ["left","right"],
                "text-justify": "left",
                "text-radial-offset": 0.5
              paint: {
                "text-color": "hsl(200, 80%,40%)",
                "text-halo-color": "white",
                "text-halo-width": 2
          function showPlaces() {
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            const category = document.getElementById("places-select").value;
            if (!category) {
              return;
                outFields: "Place_addr,PlaceName",
                params: {
                  location: map.getCenter().toArray().join(","),
                  maxLocations: 25
              .then((response) => {
                map.getSource("places").setData(response.geoJson);
              .catch((error) => {
                alert("There was a problem using the geocoder. See the console for details.");
                console.error(error);
          document.getElementById("places-select").addEventListener("change", showPlaces);
        </script>
    
      </body>
    </html>

Add controls

Use a select element to create the drop-down element. Each category is an option element inside it. The select element needs to have absolute positioning in order to appear in front of the map element.

  1. Add a select control, with options for "Coffee shop", "Gas station", "Food", "Hotel", "Parks and outdoors". Use inline styling to position the control.

    The option values have special meaning to the geocoding service so ensure the spelling is correct. See the ArcGIS REST API documentation for details.

                                                                                                                                                   
    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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    <!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;
          .places {
            position: absolute;
            top: 20px;
            left: 20px;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <select id="places-select" style="left: 20px; top: 20px; position: absolute; font-size: 16px; padding:4px 8px;">
          <option value="">Choose a category...</option>
          <option value="Coffee shop">Coffee shops</option>
          <option value="Gas station">Gas stations</option>
          <option value="Food">Food</option>
          <option value="Hotel">Hotels</option>
          <option value="Parks and Outdoors">Parks and Outdoors</option>
        </select>
    
        <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: 13,
            center: [-122.4194,37.7749] // San Francisco
          map.once("load", () => {
            map.addSource("places", {
              type: "geojson",
              data: {
                type: "FeatureCollection",
                features: []
              id: "places-circle",
              source: "places",
              type: "circle",
              paint: {
                "circle-color": "hsla(200, 80%, 80%, 0.7)",
                "circle-stroke-color": "hsl(200, 80%, 40%)",
                "circle-stroke-width": 1,
                "circle-radius": 3
              id: "places-text",
              source: "places",
              type: "symbol",
              layout: {
                "text-field": ["get", "PlaceName"],
                "text-font": ["Arial Bold"],
                "text-variable-anchor": ["left","right"],
                "text-justify": "left",
                "text-radial-offset": 0.5
              paint: {
                "text-color": "hsl(200, 80%,40%)",
                "text-halo-color": "white",
                "text-halo-width": 2
          function showPlaces() {
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            const category = document.getElementById("places-select").value;
            if (!category) {
              return;
                outFields: "Place_addr,PlaceName",
                params: {
                  location: map.getCenter().toArray().join(","),
                  maxLocations: 25
              .then((response) => {
                map.getSource("places").setData(response.geoJson);
              .catch((error) => {
                alert("There was a problem using the geocoder. See the console for details.");
                console.error(error);
          document.getElementById("places-select").addEventListener("change", showPlaces);
        </script>
    
      </body>
    </html>
  2. At the top right, click Run to verify that the select control is created and contains the different categories.

Add a load event handler

You need to wait for the map to be completely loaded before adding any layers.

  1. Add an event handler to the map load event.

                                                                                                                                                   
    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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    <!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;
          .places {
            position: absolute;
            top: 20px;
            left: 20px;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <select id="places-select" style="left: 20px; top: 20px; position: absolute; font-size: 16px; padding:4px 8px;">
          <option value="">Choose a category...</option>
          <option value="Coffee shop">Coffee shops</option>
          <option value="Gas station">Gas stations</option>
          <option value="Food">Food</option>
          <option value="Hotel">Hotels</option>
          <option value="Parks and Outdoors">Parks and Outdoors</option>
        </select>
    
        <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: 13,
    
            center: [-122.4194,37.7749] // San Francisco
    
          });
    
          map.once("load", () => {
    
            map.addSource("places", {
              type: "geojson",
              data: {
                type: "FeatureCollection",
                features: []
              id: "places-circle",
              source: "places",
              type: "circle",
              paint: {
                "circle-color": "hsla(200, 80%, 80%, 0.7)",
                "circle-stroke-color": "hsl(200, 80%, 40%)",
                "circle-stroke-width": 1,
                "circle-radius": 3
              id: "places-text",
              source: "places",
              type: "symbol",
              layout: {
                "text-field": ["get", "PlaceName"],
                "text-font": ["Arial Bold"],
                "text-variable-anchor": ["left","right"],
                "text-justify": "left",
                "text-radial-offset": 0.5
              paint: {
                "text-color": "hsl(200, 80%,40%)",
                "text-halo-color": "white",
                "text-halo-width": 2
          });
    
          function showPlaces() {
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            const category = document.getElementById("places-select").value;
            if (!category) {
              return;
                outFields: "Place_addr,PlaceName",
                params: {
                  location: map.getCenter().toArray().join(","),
                  maxLocations: 25
              .then((response) => {
                map.getSource("places").setData(response.geoJson);
              .catch((error) => {
                alert("There was a problem using the geocoder. See the console for details.");
                console.error(error);
          document.getElementById("places-select").addEventListener("change", showPlaces);
        </script>
    
      </body>
    </html>

Add a map layer

To display data on the map, there must be a source that contains the data, and a layer that determines how the source will be shown.

  1. Add a geojson source that will contain the returned results. Set its data attribute to be an empty GeoJSON FeatureCollection.

                                                                                                                                                   
    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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    <!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;
          .places {
            position: absolute;
            top: 20px;
            left: 20px;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <select id="places-select" style="left: 20px; top: 20px; position: absolute; font-size: 16px; padding:4px 8px;">
          <option value="">Choose a category...</option>
          <option value="Coffee shop">Coffee shops</option>
          <option value="Gas station">Gas stations</option>
          <option value="Food">Food</option>
          <option value="Hotel">Hotels</option>
          <option value="Parks and Outdoors">Parks and Outdoors</option>
        </select>
    
        <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: 13,
            center: [-122.4194,37.7749] // San Francisco
          map.once("load", () => {
    
            map.addSource("places", {
              type: "geojson",
              data: {
                type: "FeatureCollection",
                features: []
              }
            });
    
              id: "places-circle",
              source: "places",
              type: "circle",
              paint: {
                "circle-color": "hsla(200, 80%, 80%, 0.7)",
                "circle-stroke-color": "hsl(200, 80%, 40%)",
                "circle-stroke-width": 1,
                "circle-radius": 3
              id: "places-text",
              source: "places",
              type: "symbol",
              layout: {
                "text-field": ["get", "PlaceName"],
                "text-font": ["Arial Bold"],
                "text-variable-anchor": ["left","right"],
                "text-justify": "left",
                "text-radial-offset": 0.5
              paint: {
                "text-color": "hsl(200, 80%,40%)",
                "text-halo-color": "white",
                "text-halo-width": 2
          });
          function showPlaces() {
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            const category = document.getElementById("places-select").value;
            if (!category) {
              return;
                outFields: "Place_addr,PlaceName",
                params: {
                  location: map.getCenter().toArray().join(","),
                  maxLocations: 25
              .then((response) => {
                map.getSource("places").setData(response.geoJson);
              .catch((error) => {
                alert("There was a problem using the geocoder. See the console for details.");
                console.error(error);
          document.getElementById("places-select").addEventListener("change", showPlaces);
        </script>
    
      </body>
    </html>
  2. Add a circle layer that will display the source.

    The geojson source must have data provided. You can give it a FeatureCollection that does not contain any features. You will provide data later.

                                                                                                                                                   
    Add line.Add line.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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    <!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;
          .places {
            position: absolute;
            top: 20px;
            left: 20px;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <select id="places-select" style="left: 20px; top: 20px; position: absolute; font-size: 16px; padding:4px 8px;">
          <option value="">Choose a category...</option>
          <option value="Coffee shop">Coffee shops</option>
          <option value="Gas station">Gas stations</option>
          <option value="Food">Food</option>
          <option value="Hotel">Hotels</option>
          <option value="Parks and Outdoors">Parks and Outdoors</option>
        </select>
    
        <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: 13,
            center: [-122.4194,37.7749] // San Francisco
          map.once("load", () => {
    
            map.addSource("places", {
              type: "geojson",
              data: {
                type: "FeatureCollection",
                features: []
              }
            });
    
            map.addLayer({
              id: "places-circle",
              source: "places",
              type: "circle",
    
              paint: {
                "circle-color": "hsla(200, 80%, 80%, 0.7)",
                "circle-stroke-color": "hsl(200, 80%, 40%)",
                "circle-stroke-width": 1,
                "circle-radius": 3
              }
            });
    
              id: "places-text",
              source: "places",
              type: "symbol",
              layout: {
                "text-field": ["get", "PlaceName"],
                "text-font": ["Arial Bold"],
                "text-variable-anchor": ["left","right"],
                "text-justify": "left",
                "text-radial-offset": 0.5
              paint: {
                "text-color": "hsl(200, 80%,40%)",
                "text-halo-color": "white",
                "text-halo-width": 2
          });
          function showPlaces() {
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            const category = document.getElementById("places-select").value;
            if (!category) {
              return;
                outFields: "Place_addr,PlaceName",
                params: {
                  location: map.getCenter().toArray().join(","),
                  maxLocations: 25
              .then((response) => {
                map.getSource("places").setData(response.geoJson);
              .catch((error) => {
                alert("There was a problem using the geocoder. See the console for details.");
                console.error(error);
          document.getElementById("places-select").addEventListener("change", showPlaces);
        </script>
    
      </body>
    </html>

Call the geocoding service

You can use the arcgisRest.geocode function to retrieve places near a location. Use map.getCenter to obtain the center of the viewport in longitude and latitude.

You will also pass the category selected by the user. Use document.getElementById to get the control, then use its value property.

  1. Create a function called showPlaces. Inside, create a new arcgisRest.ApiKey to access the geocoding service. Call arcgisRest.geocode() to set the API key and to call the service endpoint. A location with Place_addr and PlaceName information will be returned based on the category selected. A maximum of 25 locations will display nearest to the center of the map.

                                                                                                                                                   
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    <!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;
          .places {
            position: absolute;
            top: 20px;
            left: 20px;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <select id="places-select" style="left: 20px; top: 20px; position: absolute; font-size: 16px; padding:4px 8px;">
          <option value="">Choose a category...</option>
          <option value="Coffee shop">Coffee shops</option>
          <option value="Gas station">Gas stations</option>
          <option value="Food">Food</option>
          <option value="Hotel">Hotels</option>
          <option value="Parks and Outdoors">Parks and Outdoors</option>
        </select>
    
        <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: 13,
            center: [-122.4194,37.7749] // San Francisco
          map.once("load", () => {
            map.addSource("places", {
              type: "geojson",
              data: {
                type: "FeatureCollection",
                features: []
              id: "places-circle",
              source: "places",
              type: "circle",
              paint: {
                "circle-color": "hsla(200, 80%, 80%, 0.7)",
                "circle-stroke-color": "hsl(200, 80%, 40%)",
                "circle-stroke-width": 1,
                "circle-radius": 3
              }
            });
    
              id: "places-text",
              source: "places",
              type: "symbol",
              layout: {
                "text-field": ["get", "PlaceName"],
                "text-font": ["Arial Bold"],
                "text-variable-anchor": ["left","right"],
                "text-justify": "left",
                "text-radial-offset": 0.5
              paint: {
                "text-color": "hsl(200, 80%,40%)",
                "text-halo-color": "white",
                "text-halo-width": 2
          });
    
          function showPlaces() {
    
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            });
            const category = document.getElementById("places-select").value;
            if (!category) {
              return;
            }
            arcgisRest
              .geocode({
                authentication,
                outFields: "Place_addr,PlaceName",
    
                params: {
                  category,
                  location: map.getCenter().toArray().join(","),
                  maxLocations: 25
                }
              })
    
              .then((response) => {
                map.getSource("places").setData(response.geoJson);
              .catch((error) => {
                alert("There was a problem using the geocoder. See the console for details.");
                console.error(error);
          }
          document.getElementById("places-select").addEventListener("change", showPlaces);
        </script>
    
      </body>
    </html>

Display results

The geoJson property of the response object contains a GeoJSON feature collection of points. You can use this to update the data of your places source.

  1. Add a response handler. Inside, use the map's setData method to set the returned value as GeoJSON.

                                                                                                                                                   
    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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    <!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;
          .places {
            position: absolute;
            top: 20px;
            left: 20px;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <select id="places-select" style="left: 20px; top: 20px; position: absolute; font-size: 16px; padding:4px 8px;">
          <option value="">Choose a category...</option>
          <option value="Coffee shop">Coffee shops</option>
          <option value="Gas station">Gas stations</option>
          <option value="Food">Food</option>
          <option value="Hotel">Hotels</option>
          <option value="Parks and Outdoors">Parks and Outdoors</option>
        </select>
    
        <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: 13,
            center: [-122.4194,37.7749] // San Francisco
          map.once("load", () => {
            map.addSource("places", {
              type: "geojson",
              data: {
                type: "FeatureCollection",
                features: []
              id: "places-circle",
              source: "places",
              type: "circle",
              paint: {
                "circle-color": "hsla(200, 80%, 80%, 0.7)",
                "circle-stroke-color": "hsl(200, 80%, 40%)",
                "circle-stroke-width": 1,
                "circle-radius": 3
              id: "places-text",
              source: "places",
              type: "symbol",
              layout: {
                "text-field": ["get", "PlaceName"],
                "text-font": ["Arial Bold"],
                "text-variable-anchor": ["left","right"],
                "text-justify": "left",
                "text-radial-offset": 0.5
              paint: {
                "text-color": "hsl(200, 80%,40%)",
                "text-halo-color": "white",
                "text-halo-width": 2
          function showPlaces() {
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            const category = document.getElementById("places-select").value;
            if (!category) {
              return;
                outFields: "Place_addr,PlaceName",
                params: {
                  category,
                  location: map.getCenter().toArray().join(","),
                  maxLocations: 25
                }
              })
    
              .then((response) => {
                map.getSource("places").setData(response.geoJson);
              })
              .catch((error) => {
                alert("There was a problem using the geocoder. See the console for details.");
                console.error(error);
          document.getElementById("places-select").addEventListener("change", showPlaces);
        </script>
    
      </body>
    </html>

Handle errors

It is good practice to handle situations where there is a problem accessing the service. This could happen due to network disruption or a problem with your API key, for instance.

  1. If there is an exception accessing the geocoding service, alert the user and log the error.

                                                                                                                                                   
    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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    <!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;
          .places {
            position: absolute;
            top: 20px;
            left: 20px;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <select id="places-select" style="left: 20px; top: 20px; position: absolute; font-size: 16px; padding:4px 8px;">
          <option value="">Choose a category...</option>
          <option value="Coffee shop">Coffee shops</option>
          <option value="Gas station">Gas stations</option>
          <option value="Food">Food</option>
          <option value="Hotel">Hotels</option>
          <option value="Parks and Outdoors">Parks and Outdoors</option>
        </select>
    
        <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: 13,
            center: [-122.4194,37.7749] // San Francisco
          map.once("load", () => {
            map.addSource("places", {
              type: "geojson",
              data: {
                type: "FeatureCollection",
                features: []
              id: "places-circle",
              source: "places",
              type: "circle",
              paint: {
                "circle-color": "hsla(200, 80%, 80%, 0.7)",
                "circle-stroke-color": "hsl(200, 80%, 40%)",
                "circle-stroke-width": 1,
                "circle-radius": 3
              id: "places-text",
              source: "places",
              type: "symbol",
              layout: {
                "text-field": ["get", "PlaceName"],
                "text-font": ["Arial Bold"],
                "text-variable-anchor": ["left","right"],
                "text-justify": "left",
                "text-radial-offset": 0.5
              paint: {
                "text-color": "hsl(200, 80%,40%)",
                "text-halo-color": "white",
                "text-halo-width": 2
          function showPlaces() {
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            const category = document.getElementById("places-select").value;
            if (!category) {
              return;
                outFields: "Place_addr,PlaceName",
                params: {
                  location: map.getCenter().toArray().join(","),
                  maxLocations: 25
              .then((response) => {
                map.getSource("places").setData(response.geoJson);
              })
    
              .catch((error) => {
                alert("There was a problem using the geocoder. See the console for details.");
                console.error(error);
              });
    
          }
          document.getElementById("places-select").addEventListener("change", showPlaces);
        </script>
    
      </body>
    </html>

Add a change event handler

To update the map when the user chooses a category, you must add an event handler for the change event on the <select> control.

  1. Update the load event handler to call the showPlaces() function.

                                                                                                                                                   
    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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    <!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;
          .places {
            position: absolute;
            top: 20px;
            left: 20px;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <select id="places-select" style="left: 20px; top: 20px; position: absolute; font-size: 16px; padding:4px 8px;">
          <option value="">Choose a category...</option>
          <option value="Coffee shop">Coffee shops</option>
          <option value="Gas station">Gas stations</option>
          <option value="Food">Food</option>
          <option value="Hotel">Hotels</option>
          <option value="Parks and Outdoors">Parks and Outdoors</option>
        </select>
    
        <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: 13,
            center: [-122.4194,37.7749] // San Francisco
          map.once("load", () => {
            map.addSource("places", {
              type: "geojson",
              data: {
                type: "FeatureCollection",
                features: []
              id: "places-circle",
              source: "places",
              type: "circle",
              paint: {
                "circle-color": "hsla(200, 80%, 80%, 0.7)",
                "circle-stroke-color": "hsl(200, 80%, 40%)",
                "circle-stroke-width": 1,
                "circle-radius": 3
            map.addLayer({
              id: "places-text",
              source: "places",
              type: "symbol",
              layout: {
                "text-field": ["get", "PlaceName"],
                "text-font": ["Arial Bold"],
                "text-variable-anchor": ["left","right"],
                "text-justify": "left",
                "text-radial-offset": 0.5
              },
              paint: {
                "text-color": "hsl(200, 80%,40%)",
                "text-halo-color": "white",
                "text-halo-width": 2
              }
            });
    
            showPlaces();
          function showPlaces() {
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            const category = document.getElementById("places-select").value;
            if (!category) {
              return;
                outFields: "Place_addr,PlaceName",
                params: {
                  location: map.getCenter().toArray().join(","),
                  maxLocations: 25
              .then((response) => {
                map.getSource("places").setData(response.geoJson);
              .catch((error) => {
                alert("There was a problem using the geocoder. See the console for details.");
                console.error(error);
          document.getElementById("places-select").addEventListener("change", showPlaces);
        </script>
    
      </body>
    </html>
  2. After the map initialization code, make the <select> control control call showPlaces() when the user chooses an option.

                                                                                                                                                   
    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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    <!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;
          .places {
            position: absolute;
            top: 20px;
            left: 20px;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <select id="places-select" style="left: 20px; top: 20px; position: absolute; font-size: 16px; padding:4px 8px;">
          <option value="">Choose a category...</option>
          <option value="Coffee shop">Coffee shops</option>
          <option value="Gas station">Gas stations</option>
          <option value="Food">Food</option>
          <option value="Hotel">Hotels</option>
          <option value="Parks and Outdoors">Parks and Outdoors</option>
        </select>
    
        <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: 13,
            center: [-122.4194,37.7749] // San Francisco
          map.once("load", () => {
            map.addSource("places", {
              type: "geojson",
              data: {
                type: "FeatureCollection",
                features: []
              id: "places-circle",
              source: "places",
              type: "circle",
              paint: {
                "circle-color": "hsla(200, 80%, 80%, 0.7)",
                "circle-stroke-color": "hsl(200, 80%, 40%)",
                "circle-stroke-width": 1,
                "circle-radius": 3
              id: "places-text",
              source: "places",
              type: "symbol",
              layout: {
                "text-field": ["get", "PlaceName"],
                "text-font": ["Arial Bold"],
                "text-variable-anchor": ["left","right"],
                "text-justify": "left",
                "text-radial-offset": 0.5
              paint: {
                "text-color": "hsl(200, 80%,40%)",
                "text-halo-color": "white",
                "text-halo-width": 2
          function showPlaces() {
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            const category = document.getElementById("places-select").value;
            if (!category) {
              return;
                outFields: "Place_addr,PlaceName",
                params: {
                  location: map.getCenter().toArray().join(","),
                  maxLocations: 25
              .then((response) => {
                map.getSource("places").setData(response.geoJson);
              .catch((error) => {
                alert("There was a problem using the geocoder. See the console for details.");
                console.error(error);
              });
    
          }
    
          document.getElementById("places-select").addEventListener("change", showPlaces);
        </script>
    
      </body>
    </html>
  3. At the top right, click Run. When you choose a place category, circles should be shown for places.

Display place labels

You can show the name of each location by using a symbol layer with text properties.

  1. In the load handler, add a symbol layer.

    It is important to use a font whose glyphs are present within the map style. This style contains several variants of Arial, which makes that a safe choice.

    By default, Mapbox GL JS centers text labels over their center point. Since there is a circle showing already, it is better to treat the point as the left anchor, and also set a text-justify of `left.

    When labels are close together, not all will be shown. You could change the default settings of text-allow-overlap if you want to alter this behavior.

    For more information about available style properties, see the Mapbox GL JS documentation.

                                                                                                                                                   
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    <!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;
          .places {
            position: absolute;
            top: 20px;
            left: 20px;
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
        <select id="places-select" style="left: 20px; top: 20px; position: absolute; font-size: 16px; padding:4px 8px;">
          <option value="">Choose a category...</option>
          <option value="Coffee shop">Coffee shops</option>
          <option value="Gas station">Gas stations</option>
          <option value="Food">Food</option>
          <option value="Hotel">Hotels</option>
          <option value="Parks and Outdoors">Parks and Outdoors</option>
        </select>
    
        <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: 13,
            center: [-122.4194,37.7749] // San Francisco
          map.once("load", () => {
            map.addSource("places", {
              type: "geojson",
              data: {
                type: "FeatureCollection",
                features: []
            map.addLayer({
              id: "places-circle",
              source: "places",
              type: "circle",
    
              paint: {
                "circle-color": "hsla(200, 80%, 80%, 0.7)",
                "circle-stroke-color": "hsl(200, 80%, 40%)",
                "circle-stroke-width": 1,
                "circle-radius": 3
              }
            });
    
            map.addLayer({
              id: "places-text",
              source: "places",
              type: "symbol",
              layout: {
                "text-field": ["get", "PlaceName"],
                "text-font": ["Arial Bold"],
                "text-variable-anchor": ["left","right"],
                "text-justify": "left",
                "text-radial-offset": 0.5
              },
              paint: {
                "text-color": "hsl(200, 80%,40%)",
                "text-halo-color": "white",
                "text-halo-width": 2
              }
            });
    
            showPlaces();
          function showPlaces() {
            const authentication = new arcgisRest.ApiKey({
              key: apiKey
            const category = document.getElementById("places-select").value;
            if (!category) {
              return;
                outFields: "Place_addr,PlaceName",
                params: {
                  location: map.getCenter().toArray().join(","),
                  maxLocations: 25
              .then((response) => {
                map.getSource("places").setData(response.geoJson);
              .catch((error) => {
                alert("There was a problem using the geocoder. See the console for details.");
                console.error(error);
          document.getElementById("places-select").addEventListener("change", showPlaces);
        </script>
    
      </body>
    </html>

Run the app

In CodePen, run your code to display the map.

When you choose a place category, you should see the name of each place shown as a label, next to a white circle with a blue outline.

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.