Learn how to search for places of interest with the ArcGIS Geocoding service
Place finding is the process of searching for a place name
Esri Leaflet provides a built in geocoder to access the Geocoding service. In this tutorial, you use the geocode operation to find places by place category.
Prerequisites
You need an ArcGIS Location Platform or ArcGIS Online account.
Steps
Get the starter app
Select a type of authentication and follow the steps to create a new app.
Choose API key authentication if you:
- Want the easiest way to get started.
- Want to build public applications
A public application is an application that allows anonymous access without requiring users to sign in with an ArcGIS account. It supports API key or app authentication. that access ArcGIS Location ServicesArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. and secure itemsAn item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. . - Have an ArcGIS Location Platform or ArcGIS Online account.
Choose user authentication if you:
- Want to build private applications.
- Require application users to sign in with their own ArcGIS account
An ArcGIS account is an identity with a user type and set of privileges that can access specific ArcGIS products, tools, APIs, services, and resources. The main account types that can be used for development are an ArcGIS Location Platform account, ArcGIS Online account, and ArcGIS Enterprise account. ArcGIS Location Platform and ArcGIS Online accounts are also associated with a subscription. and access resources their behalf. - Have an ArcGIS Online account.
To learn more about both types of authentication, go to Authentication.
Set up authentication
Set developer credentials
Use the API key or OAuth developer credentials
Reference the geocoder
-
Reference the Esri Leaflet Geocoder plugin.
Use dark colors for code blocks <!-- Load Leaflet from CDN --> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" crossorigin=""> <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" crossorigin=""></script> <!-- Load Esri Leaflet from CDN --> <script src="https://unpkg.com/esri-leaflet@3.0.19/dist/esri-leaflet.js"></script> <script src="https://unpkg.com/esri-leaflet-vector@4.3.2/dist/esri-leaflet-vector.js"></script> <!-- Load Esri Leaflet Geocoder from CDN --> <script src="https://unpkg.com/esri-leaflet-geocoder@3.1.7/dist/esri-leaflet-geocoder.js"></script>
Update the map
A navigation basemap layerarcgis/navigation.
-
Update the basemap style and change the map view to center on location
[151.2093, -33.8688], Sydney.Use dark colors for code blocks Copy /* 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_URI", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const basemapEnum = "arcgis/navigation"; const map = L.map("map", { minZoom: 2 }); map.setView([37.7749, -122.4194], 14); // San Francisco L.esri.Vector.vectorBasemapLayer(basemapEnum, { token: accessToken }).addTo(map);
Create a place category selector
You filter place search results by providing a location
-
Extend the
Controlclass to create aPlacesdropdown. Add HTML and CSS to customize its appearance.Select Use dark colors for code blocks L.esri.Vector.vectorBasemapLayer(basemapEnum, { token: accessToken }).addTo(map); L.Control.PlacesSelect = L.Control.extend({ onAdd: function () { const select = L.DomUtil.create("select", ""); select.setAttribute("id", "optionsSelect"); select.setAttribute("style", "font-size: 16px;padding:4px 8px;"); return select; }, onRemove: function () { // Nothing to do here } }); -
Create a list of
placethat be can used to make a selection.Categories Use dark colors for code blocks L.Control.PlacesSelect = L.Control.extend({ onAdd: function () { const placeCategories = [ ["", "Choose a category..."], ["Coffee shop", "Coffee shop"], ["Gas station", "Gas station"], ["Food", "Food"], ["Hotel", "Hotel"], ["Parks and Outdoors", "Parks and Outdoors"] ]; const select = L.DomUtil.create("select", ""); select.setAttribute("id", "optionsSelect"); select.setAttribute("style", "font-size: 16px;padding:4px 8px;"); return select; }, onRemove: function () { // Nothing to do here } }); -
Add each place category as an
optionin theselectelement.Use dark colors for code blocks L.Control.PlacesSelect = L.Control.extend({ onAdd: function () { const placeCategories = [ ["", "Choose a category..."], ["Coffee shop", "Coffee shop"], ["Gas station", "Gas station"], ["Food", "Food"], ["Hotel", "Hotel"], ["Parks and Outdoors", "Parks and Outdoors"] ]; const select = L.DomUtil.create("select", ""); select.setAttribute("id", "optionsSelect"); select.setAttribute("style", "font-size: 16px;padding:4px 8px;"); placeCategories.forEach((category) => { let option = L.DomUtil.create("option"); option.value = category[0]; option.innerHTML = category[1]; select.appendChild(option); }); return select; }, onRemove: function () { // Nothing to do here } }); -
Add the category selector to the
toprightcorner of the map.Use dark colors for code blocks onRemove: function () { // Nothing to do here } }); L.control.placesSelect = function (opts) { return new L.Control.PlacesSelect(opts); }; L.control.placesSelect({ position: "topright" }).addTo(map); -
Run your app and interact with the category selector, choosing between different options.
Search for place addresses
To find place addresses, you use the geocode operation. Performing a local search based on a category requires a location from which to search and a category name. The operation calls the Geocoding service and returns place candidates with a name, address and location information.
-
Create a function called
showthat takes a category selection as a parameter.Places Use dark colors for code blocks L.control.placesSelect({ position: "topright" }).addTo(map); function showPlaces(category) { } -
Call the
geocodeoperation and set youraccess. Pass the category, and set the search location to the center of the map.Token Use dark colors for code blocks function showPlaces(category) { L.esri.Geocoding .geocode({ apikey: accessToken }) .category(category) .nearby(map.getCenter(), 10) } -
Execute the request using
runand handle any errors within the callback function.Use dark colors for code blocks function showPlaces(category) { L.esri.Geocoding .geocode({ apikey: accessToken }) .category(category) .nearby(map.getCenter(), 10) .run(function (error, response) { if (error) { return; } }); }
Add a change event handler
Use an event handler to call the show function when the category is changed.
-
Add a
changeevent handler to theselectelement. Inside, call theshowfunction with the selected category as its parameter.Places Use dark colors for code blocks L.control.placesSelect({ position: "topright" }).addTo(map); function showPlaces(category) { L.esri.Geocoding .geocode({ apikey: accessToken }) .category(category) .nearby(map.getCenter(), 10) } const select = document.getElementById("optionsSelect"); select.addEventListener("change", () => { if (select.value !== "") { showPlaces(select.value); } });
Display results
You can display the results of the search with a Marker and Popup.
-
Add a
Layerto the map to contain the results.Group Use dark colors for code blocks L.control.placesSelect({ position: "topright" }).addTo(map); const layerGroup = L.layerGroup().addTo(map); function showPlaces(category) { L.esri.Geocoding .geocode({ apikey: accessToken }) .category(category) .nearby(map.getCenter(), 10) .run(function (error, response) { if (error) { return; } }); } -
In the
showfunction, call thePlaces clearmethod to remove the previous results from theLayers layer.Group Use dark colors for code blocks L.control.placesSelect({ position: "topright" }).addTo(map); const layerGroup = L.layerGroup().addTo(map); function showPlaces(category) { L.esri.Geocoding .geocode({ apikey: accessToken }) .category(category) .nearby(map.getCenter(), 10) .run(function (error, response) { if (error) { return; } layerGroup.clearLayers(); }); } -
Iterate through each search result to create a
Markerand add the markers to thelayer. Call theGroup bindmethod to display the place names and addresses in a popup.Popup Use dark colors for code blocks function showPlaces(category) { L.esri.Geocoding .geocode({ apikey: accessToken }) .category(category) .nearby(map.getCenter(), 10) .run(function (error, response) { if (error) { return; } layerGroup.clearLayers(); response.results.forEach((searchResult) => { L.marker(searchResult.latlng) .addTo(layerGroup) .bindPopup(`<b>${searchResult.properties.PlaceName}</b></br>${searchResult.properties.Place_addr}`); }); }); }
Run the app
Run the app.
When you choose a place category, you should see the places returned with pins on the map. Click each pin to view a pop-up with the place information.What's next?
Learn how to use additional location services in these tutorials:


