Tutorial: Find place addresses

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

Find place addresses

Place addresses for businesses by category with the geocoding service using API key authentication

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

Get the starter app

Select a type of authentication below and follow the steps to create a new application.

You can choose one of the following to create a new CodePen:

  • Option 1: Complete the Display a scene tutorial; or,
  • Option 2: Start from the Display a scene tutorial .

Set up authentication

Create developer credentials in your portal for the type of authentication you selected.

Create a new API key credential with the correct privileges to access the resources used in this tutorial.

  1. Go to the Create an API key tutorial and create an API key with the following privilege(s):
    • Privileges
      • Location services > Basemaps
      • Location services > Geocoding
  2. Copy the API key access token to your clipboard when prompted.

Set developer credentials

Use the API key or OAuth developer credentials created in the previous step in your application.

  1. Add <script> elements in the HTML <body> and create an accessToken variable to store your access token. Set YOUR_ACCESS_TOKEN with the access token you previously copied from your API key credentials.

    Expand
    Use dark colors for code blocks
    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
      <script>
    
        /* Use for API key authentication */
        const accessToken = "YOUR_ACCESS_TOKEN";
    
      </script>
    
    Expand
  2. Set the defaultAccessToken included with Cesium to authenticate requests to the ArcGIS services used in this tutorial.

    Expand
    Use dark colors for code blocks
    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
      <script>
    
        /* Use for API key authentication */
        const accessToken = "YOUR_ACCESS_TOKEN";
    
        Cesium.ArcGisMapService.defaultAccessToken = accessToken;
    
      </script>
    
    Expand

Get a Cesium ion access token

All Cesium applications must use an access token provided through Cesium ion. This token allows you to access assets such as Cesium World Terrain in your application.

  1. Go to your Cesium ion dashboard to generate an access token. Copy the key to your clipboard.

  2. Create a cesiumAccessToken variable and replace YOUR_CESIUM_ACCESS_TOKEN with the access token you copied from the Cesium ion dashboard.

    Use dark colors for code blocks
    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
      <script>
    
        /* Use for API key authentication */
        const accessToken = "YOUR_ACCESS_TOKEN";
    
        // or
    
        /* Use for user authentication */
        // const session = await arcgisRest.ArcGISIdentityManager.beginOAuth2({
        //   clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials
        //   redirectUri: "YOUR_REDIRECT_URL", // The redirect URL registered in your OAuth credentials
        //   portal: "YOUR_PORTAL_URL" // Your portal URL
        // })
    
        // const accessToken = session.token;
    
        Cesium.ArcGisMapService.defaultAccessToken = accessToken;
    
        const cesiumAccessToken = "YOUR_CESIUM_ACCESS_TOKEN";
    
      </script>
    
  3. Configure Cesium.Ion.defaultAccessToken with the Cesium access token to validate the application.

    Use dark colors for code blocks
    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
      <script>
    
        /* Use for API key authentication */
        const accessToken = "YOUR_ACCESS_TOKEN";
    
        // or
    
        /* Use for user authentication */
        // const session = await arcgisRest.ArcGISIdentityManager.beginOAuth2({
        //   clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials
        //   redirectUri: "YOUR_REDIRECT_URL", // The redirect URL registered in your OAuth credentials
        //   portal: "YOUR_PORTAL_URL" // Your portal URL
        // })
    
        // const accessToken = session.token;
    
        Cesium.ArcGisMapService.defaultAccessToken = accessToken;
    
        const cesiumAccessToken = "YOUR_CESIUM_ACCESS_TOKEN";
    
        Cesium.Ion.defaultAccessToken = cesiumAccessToken;
    
      </script>
    

Code

Use dark colors for code blocksCopy
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
<!DOCTYPE html>
<html lang="en">

<head>
    <title>CesiumJS: Find places</title>

    <script src="https://cesium.com/downloads/cesiumjs/releases/1.121/Build/Cesium/Cesium.js"></script>
    <link href="https://cesium.com/downloads/cesiumjs/releases/1.121/Build/Cesium/Widgets/widgets.css" rel="stylesheet">
    <!-- Load ArcGIS REST JS from CDN -->
    <script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script>
    <script
        src="https://unpkg.com/@esri/arcgis-rest-geocoding@4/dist/bundled/geocoding.umd.js"></script>
    <style>
        html,
        body,
        #cesiumContainer {
            width: 100%;
            height: 100%;
            padding: 0px;
            margin: 0px;
        }

        #places-select {
            left: 8px;
            top: 8px;
            position: absolute;
            font-size: 16px;
            padding: 4px 8px;
        }
    </style>
</head>

<body>
    <div id="cesiumContainer"></div>
    <select id="places-select">
        <option value="">Choose a place type...</option>
        <option value="Coffee shop" selected>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 type="module">

        /* Use for API key authentication */
        const accessToken = "YOUR_ACCESS_TOKEN";

        // or

        /* Use for user authentication */
        // const session = await arcgisRest.ArcGISIdentityManager.beginOAuth2({
        //   clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials
        //   redirectUri: "YOUR_REDIRECT_URL", // The redirect URL registered in your OAuth credentials
        //   portal: "https://www.arcgis.com/sharing/rest" // Your portal URL
        // })

        // const accessToken = session.token;

        Cesium.ArcGisMapService.defaultAccessToken = accessToken;

        const authentication = arcgisRest.ApiKeyManager.fromKey(accessToken);

        const cesiumAccessToken = "YOUR_CESIUM_ACCESS_TOKEN";
        Cesium.Ion.defaultAccessToken = cesiumAccessToken;

        const arcGisImagery = Cesium.ArcGisMapServerImageryProvider.fromBasemapType(Cesium.ArcGisBaseMapType.SATELLITE, {
            enablePickFeatures: false
        });

        const viewer = new Cesium.Viewer("cesiumContainer", {

            baseLayer: Cesium.ImageryLayer.fromProviderAsync(arcGisImagery),

            terrain: Cesium.Terrain.fromWorldTerrain(),
            timeline: false,
            animation: false,
            geocoder: false

        });

        // Add Esri attribution
        // Learn more in https://esriurl.com/attribution
        const poweredByEsri = new Cesium.Credit("Powered by <a href='https://www.esri.com/en-us/home' target='_blank'>Esri</a>", true)
        viewer.creditDisplay.addStaticCredit(poweredByEsri);

        viewer.camera.setView({
            destination: Cesium.Cartesian3.fromDegrees(8.6724, 50.1085, 1500)
        });

        const geoidService = await Cesium.ArcGISTiledElevationTerrainProvider.fromUrl("https://tiles.arcgis.com/tiles/GVgbJbqm8hXASVYi/arcgis/rest/services/EGM2008/ImageServer");

        // Attribution text retrieved from https://arcgis.com/home/item.html?id=d798c71512404bbb9c1551b827bf5467
        viewer.creditDisplay.addStaticCredit(new Cesium.Credit("National Geospatial-Intelligence Agency (NGA)", false));

        const i3sProvider = await Cesium.I3SDataProvider.fromUrl("https://tiles.arcgis.com/tiles/cFEFS0EWrhfDeVw9/arcgis/rest/services/Buildings_Frankfurt_2021/SceneServer", {
            geoidTiledTerrainProvider: geoidService,
            token: accessToken
        })

        // Attribution text retrieved from https://www.arcgis.com/home/item.html?id=d3344ba99c3f4efaa909ccfbcc052ed5
        viewer.creditDisplay.addStaticCredit(new Cesium.Credit("Precision Light Works (PLW)", false));

        viewer.scene.primitives.add(i3sProvider);


        function showPlaces() {
            viewer.dataSources.removeAll()
            const category = document.getElementById("places-select").value;
            const cartographic = Cesium.Cartographic.fromCartesian(viewer.camera.position);
            const center = [Cesium.Math.toDegrees(cartographic.longitude), Cesium.Math.toDegrees(cartographic.latitude)]
            arcgisRest
                .geocode({
                    authentication,
                    outFields: "Place_addr,PlaceName", // attributes to be returned

                    params: {
                        category,
                        location: center.join(','),
                        maxLocations: 25
                    }
                })
                .then((response) => {
                    const json = response.geoJson
                    Cesium.GeoJsonDataSource.load(response.geoJson, {
                        markerColor: Cesium.Color.ROYALBLUE,
                        markerSize: 48,
                        clampToGround: true
                    }).then((data) => {
                        viewer.dataSources.add(data)
                        console.log(data)
                        viewer.selectedEntity = data.entities.values[0]
                    })
                })
        }

        document.getElementById("places-select").addEventListener("change", showPlaces);

        showPlaces()
    </script>
</body>

</html>

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.