Learn how to find an address or place with a search widget and the Geocoding service.
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
widget to access the Geocoding service and perform interactive searches.
In this tutorial, you will use the Search
widget to search for addresses and places.
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.
- 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
- Privileges
- In CodePen, set
esri
to your API key..Config.api Key Use dark colors for code blocks esriConfig.apiKey = "YOUR_ACCESS_TOKEN"; const map = new Map({ basemap: "arcgis/topographic" // basemap styles service });
To learn about other ways to get an access token, go to Types of authentication.
Add modules
-
In the
require
statement, add theSearch
module.The ArcGIS Maps SDK for JavaScript is available as AMD modules and ES modules, but this tutorial is based on AMD. The AMD
require
function uses references to determine which modules will be loaded – for example, you can specify"esri/
for loading the Map module. After the modules are loaded, they are passed as parameters (e.g.Map" Map
) to the callback function where they can be used in your application. It is important to keep the module references and callback parameters in the same order. To learn more about the API's different modules visit the Overview Guide page.Use dark colors for code blocks require([ "esri/config", "esri/Map", "esri/views/MapView", "esri/widgets/Search" ], function(esriConfig, Map, MapView, Search) {
Update the map
A streets basemap layer is typically used in geocoding applications. Update the basemap
property to use the arcgis/navigation
basemap layer and change the position of the map to center on Seattle.
-
Update the
basemap
property fromarcgis/topographic
toarcgis/navigation
.Use dark colors for code blocks esriConfig.apiKey = "YOUR API KEY"; const map = new Map({ basemap: "arcgis/navigation" });
-
Update the
center
property to[-122.3321, 47.6062]
and set thezoom
property to12
to center on Seattle.Use dark colors for code blocks const view = new MapView({ container: "viewDiv", map: map, center: [-122.3321,47.6062], zoom: 12 });
Add a Search widget
The Search
widget is a visible control that 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. By default, the widget uses a locator
and source
that accesses the Geocoding service.
-
Create a
Search
widget. Set theview
property toview
.Use dark colors for code blocks const view = new MapView({ container: "viewDiv", map: map, center: [-122.3321,47.6062], zoom: 12 }); const search = new Search({ //Add Search widget view: view });
-
Add the widget to the top right corner of the view. Learn more about adding UI components to the
view
in DefaultUI.Use dark colors for code blocks const search = new Search({ //Add Search widget view: view }); view.ui.add(search, "top-right"); //Add to the map
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
widget and that allows you to interactively search for addresses and places.
What's next?
Learn how to use additional API features and ArcGIS services in these tutorials: