Learn how to search for coffee shops, gas stations, restaurants and other nearby places with the geocoding service.
Place finding is the process of searching for a place name or POI to find its address and location. You can use the geocoding service to find places such as coffee shops, gas stations, or restaurants for any geographic location around the world. You can search for places by name or by using categories. You can search near a location or you can search globally.
In this tutorial, you use ArcGIS REST JS to access the geocoding service and find places by place category.
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.
Use a select element to create the drop-down element. Each category is an option element inside it. The select element needs to have absolute positioning in order to appear in front of the map element.
Add a select control, with options for "Coffee shop", "Gas station", "Food", "Hotel", "Parks and outdoors". Use inline styling to position the control.
The option values have special meaning to the geocoding service so ensure the spelling is correct. To learn more, go to Category filtering in the ArcGIS services reference.
At the top right, click Run to verify that the select control is created and contains the different categories.
Add a places layer
To display places from the geocoding service on the map, you will add a vector layer, containing a vector source. Small blue circles is the default styling used.
Add a map load handler to the initial olms function call. Inside, create a new vector layer containing a vector source. Store it in a placesLayer, and add it to the map with map.addLayer;
To find places near a location, you can use the arcgisRest.geocode. Use the center of the map view.getCenter with proj.toLonLat to get the point in longitude and latitude. Provide the category selected by the user using document.getElementById to get the control, then use its value property.
Create a function called showPlaces. Inside, create a new arcgisRest.ApiKeyManager to access the geocoding service. Call arcgisRest.geocode() to set the API key and to call the service endpoint. Pass location and category, and specify Place_addr and PlaceName as the fields to be returned. Request a maximum of 25 places.
When the query completes, the geoJson property of the response object contains a GeoJSON feature collection of points. You can use this to update the data of your places source. You will need to use a GeoJSON feature format to read the data, and also reproject it to the map's projection. Use an exception handler to detect problems accessing the service. This could happen due to network disruption or a problem with your API key, for instance.
Add a response handler. Inside, use a new GeoJSON feature format to read the geoJson property and reproject the data. Clear the places source with clear and add the new features with addFeatures
At the top right, click Run. When you choose a place category, circles should be shown for places.
Display labels
In order to show the name of each place on the map, you will modify the layer definition to include a style property. You provide a function that takes a feature and returns a Style. It will include a Text style to display the labels, using the text property to determine which is text is displayed. Use the PlaceName property of the feature for this. You will also set properties such as textAlign and font to control the appearance of the labels. The style will also include a Circle style to draw a circle at the place location.
By default, the labels would overlap each other and be hard to read. You use the declutter property on the Vector layer to prevent this.
Add a style property to the placesLayer definition. Make this function return a new Style which includes a Circle style and a Text style for the image and text properties. Enable the declutter setting.