Learn how to find an address or place using a search box and the geocoding service.
Geocoding is the process of converting address or place text to a location. The geocoding service provides address and place geocoding and reverse geocoding.
In this tutorial, you use ArcGIS REST JS to access the geocoding service. You use a simple input control to accept text and a button to execute a search for an address or place. When an address or place is located, a pop-up will appear with the name, location, and address, and the view will pan to it.
To access location services, you need an API key or OAuth 2.0 access token. To learn how to create and scope your key, visit the Create an API key tutorial.
Go to your dashboard to get an API key. The API key must be scoped to access the services used in this tutorial.
In CodePen, update apiKey to use your key.
Use dark colors for code blocks
Change line
1
2
3
4
5
6
7
8
9
10
const apiKey = "YOUR_API_KEY";
const basemapEnum = "ArcGIS:Streets";
const map = new maplibregl.Map({
container: "map", // the id of the div elementstyle: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${basemapEnum}?type=style&token=${apiKey}`,
zoom: 12, // starting zoomcenter: [-118.805, 34.027] // starting location [longitude, latitude]});
Use an HTML <input> control to type an address into, and a <button> control to initiate the query. Wrap them in a <div>.
To add the <div> element as a Map control, you create an object that implements the MapLibre GL JS IControl interface.
Create a GeocodeControl class with an onAdd function. Inside, create a <div> element with an <input> and a <button> inside. You can use document.createElement to create a template with this HTML inside. Return this element.
When you click the button, call arcgisRest.geocode with the value of the search query. This uses the findAddressCandidates operation in the geocoding service.
For more control over geocoding, you can pass an IGeocodeOptions instead of a simple string.
Inside the click handler, create a new arcgisRest.ApiKeyManager to access the geocoding service. Call arcgisRest.geocode with the user's search query. Set the params to include the center of the map and set outFields to * to return all fields.
If the query is successful, the candidates property will contain at least one value. To display the first candidate on the map, create a pop-up and add it to the map.
Add a response handler. Inside, check that candidates contains at least one value. Use the location property to add a popup to the map and set the HTML of the popup to LongLabel to display the full address.
To add a popup to the map, you use the Popup class in MapLibre GL JS. See the Display a pop-up tutorial for more information.
Use MapLibre GL JS's panTo method to center the map at the result.
This transitions the map smoothly from the current location to the result. To instantly reposition the map to that location, you could use jumpTo instead.
It is good practice to handle situations where there is a problem accessing the service. This could happen due to network disruption or a problem with your API key, for instance.
Check for issues accessing the geocoding service and alert the user.
In the input box, type "1 King St." or "Starbucks" and then click the Geocode button to find it's location. If a location is found, the map will zoom to it and display a pop-up with the address.