Skip to content

In this tutorial you will learn how to find an address or place with a Search component and the Geocoding service .

Click on the marker to open the popup.

Geocoding is the process of converting address or place text into a location . The Geocoding service can search for an address or a place and perform reverse geocoding . Use the Search component to access the Geocoding service and perform interactive searches.

Prerequisites

Steps

Create a new pen

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

Get an access token

You need an access token with the correct privileges to access the location services 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
  2. In CodePen, set the apiKey property on the global esriConfig variable to your access token.
    <!-- The esriConfig variable must be defined before adding the other esri libraries -->
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>

To learn about other ways to get an access token, go to Types of authentication.

Add Search component

The Search component allows you to interactively search for addresses and places. It provides suggestions as you type and allows you to select a result. When you select a result, it zooms to a location and displays a pop-up with the address information.

  1. Inside the <arcgis-map> component, add the <arcgis-search> component and specify where you want to position it using the slot attribute.

    29 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Search for an address</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/community" center="-118.49178, 34.01185" zoom="15">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <arcgis-search slot="top-right"></arcgis-search>
    </arcgis-map>
    50 collapsed lines
    <script type="module">
    const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
    // Get a reference to the map component
    const viewElement = document.querySelector("arcgis-map");
    // Wait for when the component is ready
    await viewElement.viewOnReady();
    const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    viewElement.addEventListener("arcgisViewClick", (event) => {
    const params = {
    location: event.detail.mapPoint,
    };
    locator.locationToAddress(serviceUrl, params).then(
    (response) => {
    // Show the address
    const address = response.address;
    showAddress(address, event.detail.mapPoint);
    },
    (error) => {
    // Show no address found
    showAddress("No address found.", event.detail.mapPoint);
    },
    );
    });
    function showAddress(address, point) {
    viewElement.openPopup({
    title:
    + Math.round(point.longitude * 100000) / 100000 +
    ", " +
    Math.round(point.latitude * 100000) / 100000,
    content: address,
    location: point,
    });
    }
    </script>
    </body>
    </html>

Add modules and reference map component

  1. Use $arcgis.import() to add the locator module to the app. This module provides helper functions for working with geocoding.
37 collapsed lines
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>ArcGIS Maps SDK for JavaScript Tutorials: Search for an address</title>
<style>
html,
body {
height: 100%;
margin: 0;
}
</style>
<script>
var esriConfig = {
apiKey: "YOUR_ACCESS_TOKEN",
};
</script>
<!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
<script type="module" src="https://js.arcgis.com/5.0/"></script>
</head>
<body>
<arcgis-map basemap="arcgis/community" center="-118.49178, 34.01185" zoom="15">
<arcgis-zoom slot="top-left"></arcgis-zoom>
<arcgis-search slot="top-right"></arcgis-search>
</arcgis-map>
<script type="module">
const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
// Get a reference to the map component
const viewElement = document.querySelector("arcgis-map");
// Wait for when the component is ready
await viewElement.viewOnReady();
</script>
5 collapsed lines
</body>
</html>

Add the geocoding service URL

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

  1. Define a variable, serviceUrl, then add a reference to the Geocoding service URL.
    48 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Search for an address</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/community" center="-118.49178, 34.01185" zoom="15">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <arcgis-search slot="top-right"></arcgis-search>
    </arcgis-map>
    <script type="module">
    const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
    // Get a reference to the map component
    const viewElement = document.querySelector("arcgis-map");
    // Wait for when the component is ready
    await viewElement.viewOnReady();
    const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    37 collapsed lines
    viewElement.addEventListener("arcgisViewClick", (event) => {
    const params = {
    location: event.detail.mapPoint,
    };
    locator.locationToAddress(serviceUrl, params).then(
    (response) => {
    // Show the address
    const address = response.address;
    showAddress(address, event.detail.mapPoint);
    },
    (error) => {
    // Show no address found
    showAddress("No address found.", event.detail.mapPoint);
    },
    );
    });
    function showAddress(address, point) {
    viewElement.openPopup({
    title:
    + Math.round(point.longitude * 100000) / 100000 +
    ", " +
    Math.round(point.latitude * 100000) / 100000,
    content: address,
    location: point,
    });
    }
    </script>
    </body>
    </html>

Reverse geocode

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

  1. Add an arcgisViewClick event handler to the map component. Inside the callback, define an object named params. Assign to it a key named location and set they key’s value to evt.detail.mapPoint.

    48 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Search for an address</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/community" center="-118.49178, 34.01185" zoom="15">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <arcgis-search slot="top-right"></arcgis-search>
    </arcgis-map>
    <script type="module">
    const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
    // Get a reference to the map component
    const viewElement = document.querySelector("arcgis-map");
    // Wait for when the component is ready
    await viewElement.viewOnReady();
    const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    viewElement.addEventListener("arcgisViewClick", (event) => {
    const params = {
    location: event.detail.mapPoint,
    };
    });
    7 collapsed lines
    </script>
    </body>
    </html>
  2. Create a showAddress function to display coordinates and the corresponding address within a pop-up .

    50 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Search for an address</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/community" center="-118.49178, 34.01185" zoom="15">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <arcgis-search slot="top-right"></arcgis-search>
    </arcgis-map>
    <script type="module">
    const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
    // Get a reference to the map component
    const viewElement = document.querySelector("arcgis-map");
    // Wait for when the component is ready
    await viewElement.viewOnReady();
    const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    function showAddress(address, point) {
    viewElement.openPopup({
    title:
    + Math.round(point.longitude * 100000) / 100000 +
    ", " +
    Math.round(point.latitude * 100000) / 100000,
    content: address,
    location: point,
    });
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  3. Reverse geocode the point that is created by click on the map. Add a call to the locator’s locationToAddress function. Use the showAddress function to display a pop-up with the results.

    50 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Search for an address</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/community" center="-118.49178, 34.01185" zoom="15">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <arcgis-search slot="top-right"></arcgis-search>
    </arcgis-map>
    <script type="module">
    const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
    // Get a reference to the map component
    const viewElement = document.querySelector("arcgis-map");
    // Wait for when the component is ready
    await viewElement.viewOnReady();
    const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    viewElement.addEventListener("arcgisViewClick", (event) => {
    const params = {
    location: event.detail.mapPoint,
    };
    locator.locationToAddress(serviceUrl, params).then(
    (response) => {
    // Show the address
    const address = response.address;
    showAddress(address, event.detail.mapPoint);
    },
    (error) => {
    // Show no address found
    showAddress("No address found.", event.detail.mapPoint);
    },
    );
    });
    18 collapsed lines
    function showAddress(address, point) {
    viewElement.openPopup({
    title:
    + Math.round(point.longitude * 100000) / 100000 +
    ", " +
    Math.round(point.latitude * 100000) / 100000,
    content: address,
    location: point,
    });
    }
    </script>
    </body>
    </html>

Run the app

In CodePen, run your code to display the map.

Try searching for the following locations: - Seattle - Space Needle - Hollywood Blvd - 34.13419, -118.29636.

The map should display a Search component that allows you to interactively search for addresses and places . When a result is available the component zooms the map to the associated location and automatically creates a popup.

What’s next?

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