Learn how to search for an address with autosuggest.
The ArcGIS Geocoding service
In this tutorial, use the MapLibre ArcGIS plugin to display a map and ArcGIS REST JS to access the ArcGIS Geocoding service
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
Add script references
-
In the
<headelement, add references to the ArcGIS REST JS> requestandgeocodinglibraries.Use dark colors for code blocks <!-- Load MapLibre GL JS from CDN --> <script src="https://unpkg.com/maplibre-gl@5.23.0/dist/maplibre-gl.js"></script> <link href="https://unpkg.com/maplibre-gl@5.23.0/dist/maplibre-gl.css" rel="stylesheet"> <!-- Load MapLibre ArcGIS from CDN --> <script src="https://unpkg.com/@esri/maplibre-arcgis@1.1.0/dist/umd/maplibre-arcgis.min.js"></script> <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>
Update the map
An arcgis/navigation basemap style
-
Update the basemap and initialize the map to center on the coordinates
[-123.102, 49.234]with a zoom level of11. This will center the map on Vancouver.Use dark colors for code blocks Copy const map = new maplibregl.Map({ container: "map", zoom: 11, center: [-123.102, 49.234] // Vancouver }); const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, { style: "arcgis/navigation", token: accessToken });
Create geocoding controls
Create an HTML <div containing an <input for typing an address and a <button to clear the input. Implement this <div as a as a Map control using the IControl interface. Then, in the <body of your HTML, add a <ul element to display address suggestions as the user types.
-
In the
<bodyof your HTML, add a> <ulelement with the id> suggestions.Use dark colors for code blocks <div id="map"></div> <ul id="suggestions"></ul> -
Create a
Geocodeclass with anControl onfunction. Inside, create aAdd <divelement with an> <inputand a> <buttoninside. Then, add the> geocodeelement to theControl top-leftof the view.Use dark colors for code blocks class GeocodeControl { onAdd() { const container = document.createElement("div"); container.className = "maplibregl-ctrl maplibregl-ctrl-group"; container.innerHTML = ` <div id="geocode-container"> <input id="geocode-input" type="text" placeholder="Type an address" autocomplete="off" /> <button id="clear-button">×</button> </div> `; return container; } } map.addControl(new GeocodeControl(), "top-left"); -
Add styling to the controls.
Use dark colors for code blocks <style> html, body, #map { padding: 0; margin: 0; height: 100%; width: 100%; font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #323232; } #geocode-container { display: flex; align-items: center; } #geocode-input { padding: 4px 8px; width: 20vw; } #clear-button { border: none; color: #666; cursor: pointer; background-color: #fff; } #suggestions { display: none; position: absolute; top: 40px; left: 10px; background: white; list-style: none; padding: 0; margin: 0; width: 25vw; z-index: 999; } #suggestions li { padding: 6px 8px; border: 1px solid transparent; box-sizing: border-box; } #suggestions li:hover { background-color: #f0f0f0; } #suggestions li.selected { border-color: #000000; } .maplibregl-ctrl-bottom-right { margin-left: 50px; } </style> -
Set up interactivity for the geocoding controls by doing the following:
- Handling hover styles.
- Enabling keyboard navigation.
- Managing suggestion visibility with clear and outside-click actions.
Use dark colors for code blocks const suggestionsList = document.getElementById("suggestions"); let selectedIndex = -1; function updateSelection(items) { items.forEach((item, i) => item.classList.toggle("selected", i === selectedIndex)); } input.addEventListener("keydown", (e) => { const items = suggestionsList.querySelectorAll("li"); if (e.key === "ArrowDown") { if (selectedIndex < items.length - 1) selectedIndex++; } else if (e.key === "ArrowUp") { if (selectedIndex > 0) selectedIndex--; } else if (e.key === "Enter" && selectedIndex >= 0) { items[selectedIndex].click(); } updateSelection(items); }); document.getElementById("clear-button").addEventListener("click", () => { input.value = ""; popup.remove(); }); document.addEventListener("click", (e) => { if (!document.getElementById("geocode-container").contains(e.target)) { suggestionsList.style.display = "none"; } });
Get address suggestions
Use the suggest method from the geocoding service to fetch location suggestions as a user types.
-
Add an input event handler to capture text as a user types. Reset the selection index and hide suggestions when the input is empty.
Use dark colors for code blocks input.addEventListener("input", () => { selectedIndex = -1; const text = input.value; if (!text) return suggestionsList.style.display = "none"; }); -
Use the
suggestmethod to fetch location suggestions as the user types, then display them in a list. For each suggestion, create an<lielement, append it to the suggestions list, and make the list visible.> Use dark colors for code blocks input.addEventListener("input", () => { selectedIndex = -1; const text = input.value; if (!text) return suggestionsList.style.display = "none"; arcgisRest.suggest(text, { authentication, params: { location: { x: -123.024, y: 49.274, spatialReference: { wkid: 4326 } }, maxSuggestions: 5 } }).then((response) => { suggestionsList.innerHTML = ""; if (!response.suggestions || response.suggestions.length === 0) { suggestionsList.style.display = "none"; return; } response.suggestions.forEach((s) => { const li = document.createElement("li"); li.textContent = s.text; suggestionsList.appendChild(li); }); suggestionsList.style.display = "block"; }); });
Call the geocode method
When the user selects a suggestion, call the geocode method to get location coordinates.
-
Get the input field and initialize the popup.
Use dark colors for code blocks const input = document.getElementById("geocode-input"); let popup = null; -
Use your access token to authenticate with ArcGIS REST JS and make requests to the geocoding service.
Use dark colors for code blocks const authentication = arcgisRest.ApiKeyManager.fromKey(accessToken); -
Create a function that calls
geocodeusing themagicreturned by theKey suggestmethod. Themagichelps match partial text input to a complete address. You will use this function in a later step.Key Use dark colors for code blocks function performGeocode(magicKey) { arcgisRest.geocode({ magicKey: magicKey, authentication }).then((response) => { const result = response.candidates[0]; if (!result) return; const lngLat = [result.location.x, result.location.y]; }); } -
Add a click handler to each suggestion item to call the
performfunction with the suggestion'sGeocode magic.Key Use dark colors for code blocks li.addEventListener("click", () => { input.value = s.text; suggestionsList.style.display = "none"; performGeocode(s.magicKey); });
Show the result
- Show the location details with a popup and pan the map to the location.
Use dark colors for code blocks function performGeocode(magicKey) { arcgisRest.geocode({ magicKey: magicKey, authentication }).then((response) => { const result = response.candidates[0]; if (!result) return; const lngLat = [result.location.x, result.location.y]; popup?.remove(); popup = new maplibregl.Popup({ focusAfterOpen: false }) .setLngLat(lngLat) .setHTML(`<strong>${result.address}</strong><br />${lngLat[0].toFixed(4)}, ${lngLat[1].toFixed(4)}`) .addTo(map); const bounds = [ [result.extent.xmin, result.extent.ymin], // [south, west] [result.extent.xmax, result.extent.ymax] // [north, east] ]; map.fitBounds(bounds, { padding: 20, animate: false }); }); }
Run the app
Run the app.
In the input box, type a partial address and see the suggestions come up. Click on a suggestion and the map will pan to the geocoded address and show a popup with the location details.What's next?
Learn how to use additional location services in these tutorials:


