Learn how to find an address or place with a search bar and the geocoding service A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. Learn more .

search for an address

Geocoding is the process of converting address or place A place, also known as a point of interest (POI), is a location that represents a business, administrative entity, or geographic feature around the world. A place can also have attributes associated with it, such as a name, address, category, and ID. Learn more text into a location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more . The geocoding service A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. Learn more can search for an address or a place and perform reverse geocoding Reverse geocoding is the process of converting a point to its nearest address or place. Learn more .

In this tutorial, you use a search bar in the user interface to access the Geocoding service and search for addresses and places.

Prerequisites

Before starting this tutorial:

  1. You need an ArcGIS Location Platform or ArcGIS Online account.

  2. Confirm that your system meets the minimum system requirements.

  3. An IDE for Java.

Steps

Get an access token

You need an access token An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. Learn more to use the location services ArcGIS 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. Learn more used in this tutorial.

  1. Go to the Create an API key tutorial to obtain an access token An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. Learn more using your ArcGIS Location Platform An ArcGIS Location Platform account, formerly known as an ArcGIS Developer account, is an identity associated with an ArcGIS Location Platform subscription. Learn more or ArcGIS Online An ArcGIS Online account, also known as an ArcGIS Organization account, is an identity associated with an ArcGIS Online subscription. It can be used to access ArcGIS tools and develop applications with ArcGIS location services for an organization. Learn more account.

  2. Ensure that the following privileges Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more are enabled: Location services > Basemaps > Basemap styles service and Location services > Geocoding.

  3. Copy the access token as it will be used in the next step.

To learn more about other ways to get an access token, go to Types of authentication.

Open a Java project with Gradle

  1. To start this tutorial, complete the Display a map tutorial, or download and unzip the Display a map solution into a new folder.

  2. Open the build.gradle file as a project in IntelliJ IDEA.

  3. In IntelliJ IDEA’s Project tool window, open src/main/java/com.example.app and double-click App.

  4. In the start() method, set the API key property on the ArcGISRuntimeEnvironment with your access token An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. Learn more . Replace YOUR_ACCESS_TOKEN with your copied access token. Be sure to surround your access token with double quotes as it is a string.

    App.java
    ArcGISRuntimeEnvironment.setApiKey("YOUR_ACCESS_TOKEN");

Add import statements and variable declarations

Add import statements and variable declarations to reference the packages and classes required for this tutorial.

  1. In IntelliJ IDEA’s Project tool window, open src/main/java/com.example.app and double-click App.

  2. Add the following imports above the existing imports:

    App.java
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.paint.Color;
    import javafx.scene.control.Alert;
    import javafx.scene.control.TextField;
    import com.esri.arcgisruntime.concurrent.ListenableFuture;
    import com.esri.arcgisruntime.mapping.view.Graphic;
    import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
    import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
    import com.esri.arcgisruntime.symbology.TextSymbol;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
    import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
  3. Within the App class, add the following member variables to easily reference them from other parts of the application:

    App.java
    public class App extends Application {
    private MapView mapView;
    private GeocodeParameters geocodeParameters;
    private GraphicsOverlay graphicsOverlay;
    private LocatorTask locatorTask;
    private TextField searchBox;

Add a graphics overlay

A graphics overlay A graphics overlay is a client-side, temporary container of graphics to display on a map view or scene view. Learn more is a container for graphics A graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. Learn more . A graphic will be added later in this tutorial as a visual means to display the search result on the map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more .

  1. In the start() method, create a new GraphicsOverlay and add it to the mapView.

    App.java
    80 collapsed lines
    // Copyright 2020 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    package com.example.app;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.paint.Color;
    import javafx.scene.control.Alert;
    import javafx.scene.control.TextField;
    import com.esri.arcgisruntime.concurrent.ListenableFuture;
    import com.esri.arcgisruntime.mapping.view.Graphic;
    import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
    import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
    import com.esri.arcgisruntime.symbology.TextSymbol;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
    import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    public class App extends Application {
    private MapView mapView;
    private GeocodeParameters geocodeParameters;
    private GraphicsOverlay graphicsOverlay;
    private LocatorTask locatorTask;
    private TextField searchBox;
    public static void main(String[] args) {
    Application.launch(args);
    }
    @Override
    public void start(Stage stage) {
    // set the title and size of the stage and show it
    stage.setTitle("Search for an address tutorial");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create a JavaFX scene with a stack pane as the root node
    // and add it to the scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    ArcGISRuntimeEnvironment.setApiKey("YOUR_ACCESS_TOKEN");
    // create a map view to display the map and add it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 144447.638572));
    // create a graphics overlay and add it to the map view
    graphicsOverlay = new GraphicsOverlay();
    mapView.getGraphicsOverlays().add(graphicsOverlay);
    14 collapsed lines
    }
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    }

Add a UI for user input

To search an address using the application, add a UI element to prompt the user for text input. The text input will be used as the geocode search text in a later step.

  1. Create a new private method named setupTextField().

  2. Within the method body, create a JavaFX TextField, assign it to the searchBox member variable, and set its width and prompt text.

    App.java
    88 collapsed lines
    // Copyright 2020 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    package com.example.app;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.paint.Color;
    import javafx.scene.control.Alert;
    import javafx.scene.control.TextField;
    import com.esri.arcgisruntime.concurrent.ListenableFuture;
    import com.esri.arcgisruntime.mapping.view.Graphic;
    import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
    import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
    import com.esri.arcgisruntime.symbology.TextSymbol;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
    import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    public class App extends Application {
    private MapView mapView;
    private GeocodeParameters geocodeParameters;
    private GraphicsOverlay graphicsOverlay;
    private LocatorTask locatorTask;
    private TextField searchBox;
    public static void main(String[] args) {
    Application.launch(args);
    }
    @Override
    public void start(Stage stage) {
    // set the title and size of the stage and show it
    stage.setTitle("Search for an address tutorial");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create a JavaFX scene with a stack pane as the root node
    // and add it to the scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    ArcGISRuntimeEnvironment.setApiKey("YOUR_ACCESS_TOKEN");
    // create a map view to display the map and add it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 144447.638572));
    // create a graphics overlay and add it to the map view
    graphicsOverlay = new GraphicsOverlay();
    mapView.getGraphicsOverlays().add(graphicsOverlay);
    }
    private void setupTextField() {
    searchBox = new TextField();
    searchBox.setMaxWidth(400);
    searchBox.setPromptText("Search for an address");
    }
    12 collapsed lines
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    }
  3. Call the setupTextField() method in the start() method.

    App.java
    88 collapsed lines
    // Copyright 2020 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    package com.example.app;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.paint.Color;
    import javafx.scene.control.Alert;
    import javafx.scene.control.TextField;
    import com.esri.arcgisruntime.concurrent.ListenableFuture;
    import com.esri.arcgisruntime.mapping.view.Graphic;
    import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
    import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
    import com.esri.arcgisruntime.symbology.TextSymbol;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
    import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    public class App extends Application {
    private MapView mapView;
    private GeocodeParameters geocodeParameters;
    private GraphicsOverlay graphicsOverlay;
    private LocatorTask locatorTask;
    private TextField searchBox;
    public static void main(String[] args) {
    Application.launch(args);
    }
    @Override
    public void start(Stage stage) {
    // set the title and size of the stage and show it
    stage.setTitle("Search for an address tutorial");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create a JavaFX scene with a stack pane as the root node
    // and add it to the scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    ArcGISRuntimeEnvironment.setApiKey("YOUR_ACCESS_TOKEN");
    // create a map view to display the map and add it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 144447.638572));
    // create a graphics overlay and add it to the map view
    graphicsOverlay = new GraphicsOverlay();
    mapView.getGraphicsOverlays().add(graphicsOverlay);
    setupTextField();
    20 collapsed lines
    }
    private void setupTextField() {
    searchBox = new TextField();
    searchBox.setMaxWidth(400);
    searchBox.setPromptText("Search for an address");
    }
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    }
  4. Add the searchBox to the top left of the map, setting its alignment and margins as shown:

    App.java
    90 collapsed lines
    // Copyright 2020 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    package com.example.app;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.paint.Color;
    import javafx.scene.control.Alert;
    import javafx.scene.control.TextField;
    import com.esri.arcgisruntime.concurrent.ListenableFuture;
    import com.esri.arcgisruntime.mapping.view.Graphic;
    import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
    import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
    import com.esri.arcgisruntime.symbology.TextSymbol;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
    import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    public class App extends Application {
    private MapView mapView;
    private GeocodeParameters geocodeParameters;
    private GraphicsOverlay graphicsOverlay;
    private LocatorTask locatorTask;
    private TextField searchBox;
    public static void main(String[] args) {
    Application.launch(args);
    }
    @Override
    public void start(Stage stage) {
    // set the title and size of the stage and show it
    stage.setTitle("Search for an address tutorial");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create a JavaFX scene with a stack pane as the root node
    // and add it to the scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    ArcGISRuntimeEnvironment.setApiKey("YOUR_ACCESS_TOKEN");
    // create a map view to display the map and add it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 144447.638572));
    // create a graphics overlay and add it to the map view
    graphicsOverlay = new GraphicsOverlay();
    mapView.getGraphicsOverlays().add(graphicsOverlay);
    setupTextField();
    stackPane.getChildren().add(searchBox);
    StackPane.setAlignment(searchBox, Pos.TOP_LEFT);
    StackPane.setMargin(searchBox, new Insets(10, 0, 0, 10));
    }
    18 collapsed lines
    private void setupTextField() {
    searchBox = new TextField();
    searchBox.setMaxWidth(400);
    searchBox.setPromptText("Search for an address");
    }
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    }

Create a locator task with geocode parameters

Geocoding is implemented with a locator A locator is an ArcGIS dataset that stores address information and the rules for translating descriptions of places (such as street addresses or place names) into spatial data that can be displayed on a map. Learn more , typically created by referencing a service such as the Geocoding service A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. Learn more or, for offline geocoding, by referencing locator data contained in a mobile package A mobile package is a stand-alone Mobile Map Package (MMPK) or Mobile Scene Package (MSPK) file for use in offline applications built with ArcGIS Maps SDKs for Native Apps. Learn more . Geocoding parameters can be used to fine-tune the results, such as setting the maximum number of results or requesting additional attributes in the results.

  1. Create a new private method named createLocatorTaskAndDefaultParameters().

  2. Within the method body, create a new LocatorTask based on the Geocoding service, and assign it to the locatorTask member variable.

  3. Create new GeocodeParameters, and assign it to the geocodeParameters member variable. Specify the geocode’s attributes as follows:

    • Specify which attributes to return by calling the .add() method on the GeocodeParameters.getResultAttributeNames(). * is used to return all attributes.
    • Set the maximum number of results to be returned with GeocodeParameters.setMaxResults(). In this tutorial, only return the best match by passing in 1. Results are ordered by score, so returning only the first result will return the highest scoring result.
    • Set the spatial reference A spatial reference is a set of parameters, typically defined by a WKID, that define the coordinate system and spatial properties for geographic data. Applications use a spatial reference to correctly display the position of geographic data in a map or scene. Learn more with GeocodeParameters.setOutputSpatialReference(). By default the output spatial reference is determined by the geocode service. For optimal performance when displaying the geocode result, ensure the returned coordinates match those of the map view by providing mapView.getSpatialReference() as a parameter.
    App.java
    98 collapsed lines
    // Copyright 2020 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    package com.example.app;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.paint.Color;
    import javafx.scene.control.Alert;
    import javafx.scene.control.TextField;
    import com.esri.arcgisruntime.concurrent.ListenableFuture;
    import com.esri.arcgisruntime.mapping.view.Graphic;
    import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
    import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
    import com.esri.arcgisruntime.symbology.TextSymbol;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
    import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    public class App extends Application {
    private MapView mapView;
    private GeocodeParameters geocodeParameters;
    private GraphicsOverlay graphicsOverlay;
    private LocatorTask locatorTask;
    private TextField searchBox;
    public static void main(String[] args) {
    Application.launch(args);
    }
    @Override
    public void start(Stage stage) {
    // set the title and size of the stage and show it
    stage.setTitle("Search for an address tutorial");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create a JavaFX scene with a stack pane as the root node
    // and add it to the scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    ArcGISRuntimeEnvironment.setApiKey("YOUR_ACCESS_TOKEN");
    // create a map view to display the map and add it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 144447.638572));
    // create a graphics overlay and add it to the map view
    graphicsOverlay = new GraphicsOverlay();
    mapView.getGraphicsOverlays().add(graphicsOverlay);
    setupTextField();
    stackPane.getChildren().add(searchBox);
    StackPane.setAlignment(searchBox, Pos.TOP_LEFT);
    StackPane.setMargin(searchBox, new Insets(10, 0, 0, 10));
    }
    private void setupTextField() {
    searchBox = new TextField();
    searchBox.setMaxWidth(400);
    searchBox.setPromptText("Search for an address");
    }
    private void createLocatorTaskAndDefaultParameters() {
    locatorTask = new LocatorTask("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer");
    geocodeParameters = new GeocodeParameters();
    geocodeParameters.getResultAttributeNames().add("*");
    geocodeParameters.setMaxResults(1);
    geocodeParameters.setOutputSpatialReference(mapView.getSpatialReference());
    }
    12 collapsed lines
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    }
  4. Call the createLocatorTaskAndDefaultParameters() method in the start() method.

    App.java
    90 collapsed lines
    // Copyright 2020 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    package com.example.app;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.paint.Color;
    import javafx.scene.control.Alert;
    import javafx.scene.control.TextField;
    import com.esri.arcgisruntime.concurrent.ListenableFuture;
    import com.esri.arcgisruntime.mapping.view.Graphic;
    import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
    import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
    import com.esri.arcgisruntime.symbology.TextSymbol;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
    import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    public class App extends Application {
    private MapView mapView;
    private GeocodeParameters geocodeParameters;
    private GraphicsOverlay graphicsOverlay;
    private LocatorTask locatorTask;
    private TextField searchBox;
    public static void main(String[] args) {
    Application.launch(args);
    }
    @Override
    public void start(Stage stage) {
    // set the title and size of the stage and show it
    stage.setTitle("Search for an address tutorial");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create a JavaFX scene with a stack pane as the root node
    // and add it to the scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    ArcGISRuntimeEnvironment.setApiKey("YOUR_ACCESS_TOKEN");
    // create a map view to display the map and add it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 144447.638572));
    // create a graphics overlay and add it to the map view
    graphicsOverlay = new GraphicsOverlay();
    mapView.getGraphicsOverlays().add(graphicsOverlay);
    setupTextField();
    createLocatorTaskAndDefaultParameters();
    24 collapsed lines
    stackPane.getChildren().add(searchBox);
    StackPane.setAlignment(searchBox, Pos.TOP_LEFT);
    StackPane.setMargin(searchBox, new Insets(10, 0, 0, 10));
    }
    private void setupTextField() {
    searchBox = new TextField();
    searchBox.setMaxWidth(400);
    searchBox.setPromptText("Search for an address");
    }
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    }

Get the geocode results with a geocode operation

An asynchronous geocode operation is required to find and return the location candidates for a given address and geocode parameters.

  1. Create a new private method named performGeocode() that takes a String parameter for the address.

  2. To find the location for a given address, call the LocatorTask.geocodeAsync() method on the locatorTask, providing the address string and geocodeParameters as parameters, and store the result in a local variable named geocodeResults. The result is an immutable list of GeocodeResult objects. In this tutorial, only one result will be returned, as the maximum results parameter was set to 1. Get the location result by calling an ListenableFuture.addDoneListener() on the geocodeResults. The result will be displayed visually in the next step.

    App.java
    106 collapsed lines
    // Copyright 2020 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    package com.example.app;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.paint.Color;
    import javafx.scene.control.Alert;
    import javafx.scene.control.TextField;
    import com.esri.arcgisruntime.concurrent.ListenableFuture;
    import com.esri.arcgisruntime.mapping.view.Graphic;
    import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
    import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
    import com.esri.arcgisruntime.symbology.TextSymbol;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
    import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    public class App extends Application {
    private MapView mapView;
    private GeocodeParameters geocodeParameters;
    private GraphicsOverlay graphicsOverlay;
    private LocatorTask locatorTask;
    private TextField searchBox;
    public static void main(String[] args) {
    Application.launch(args);
    }
    @Override
    public void start(Stage stage) {
    // set the title and size of the stage and show it
    stage.setTitle("Search for an address tutorial");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create a JavaFX scene with a stack pane as the root node
    // and add it to the scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    ArcGISRuntimeEnvironment.setApiKey("YOUR_ACCESS_TOKEN");
    // create a map view to display the map and add it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 144447.638572));
    // create a graphics overlay and add it to the map view
    graphicsOverlay = new GraphicsOverlay();
    mapView.getGraphicsOverlays().add(graphicsOverlay);
    setupTextField();
    createLocatorTaskAndDefaultParameters();
    stackPane.getChildren().add(searchBox);
    StackPane.setAlignment(searchBox, Pos.TOP_LEFT);
    StackPane.setMargin(searchBox, new Insets(10, 0, 0, 10));
    }
    private void setupTextField() {
    searchBox = new TextField();
    searchBox.setMaxWidth(400);
    searchBox.setPromptText("Search for an address");
    }
    private void createLocatorTaskAndDefaultParameters() {
    locatorTask = new LocatorTask("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer");
    geocodeParameters = new GeocodeParameters();
    geocodeParameters.getResultAttributeNames().add("*");
    geocodeParameters.setMaxResults(1);
    geocodeParameters.setOutputSpatialReference(mapView.getSpatialReference());
    }
    private void performGeocode(String address) {
    ListenableFuture<List<GeocodeResult>> geocodeResults = locatorTask.geocodeAsync(address, geocodeParameters);
    geocodeResults.addDoneListener(() -> {
    try {
    List<GeocodeResult> geocodes = geocodeResults.get();
    if (geocodes.size() > 0) {
    GeocodeResult result = geocodes.get(0);
    } else {
    new Alert(Alert.AlertType.INFORMATION, "No results found.").show();
    }
    } catch (InterruptedException | ExecutionException e) {
    new Alert(Alert.AlertType.ERROR, "Error getting result.").show();
    e.printStackTrace();
    }
    });
    }
    12 collapsed lines
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    }

Display the result

The result obtained from the geocode operation can be displayed by adding a graphic A graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. Learn more to the map view’s graphics overlay A graphics overlay is a client-side, temporary container of graphics to display on a map view or scene view. Learn more .

  1. Create a new private method taking a GeocodeResult (the result to be displayed on the map) as a parameter, and name it displayResult().

  2. Within the method body, create and style two Graphic objects and add them to the graphicsOverlay. Use the geocodeResult’s label and display location to add one graphic showing the geocode result’s label text (the address), and another showing the geocode result’s location.

    App.java
    124 collapsed lines
    // Copyright 2020 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    package com.example.app;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.paint.Color;
    import javafx.scene.control.Alert;
    import javafx.scene.control.TextField;
    import com.esri.arcgisruntime.concurrent.ListenableFuture;
    import com.esri.arcgisruntime.mapping.view.Graphic;
    import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
    import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
    import com.esri.arcgisruntime.symbology.TextSymbol;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
    import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    public class App extends Application {
    private MapView mapView;
    private GeocodeParameters geocodeParameters;
    private GraphicsOverlay graphicsOverlay;
    private LocatorTask locatorTask;
    private TextField searchBox;
    public static void main(String[] args) {
    Application.launch(args);
    }
    @Override
    public void start(Stage stage) {
    // set the title and size of the stage and show it
    stage.setTitle("Search for an address tutorial");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create a JavaFX scene with a stack pane as the root node
    // and add it to the scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    ArcGISRuntimeEnvironment.setApiKey("YOUR_ACCESS_TOKEN");
    // create a map view to display the map and add it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 144447.638572));
    // create a graphics overlay and add it to the map view
    graphicsOverlay = new GraphicsOverlay();
    mapView.getGraphicsOverlays().add(graphicsOverlay);
    setupTextField();
    createLocatorTaskAndDefaultParameters();
    stackPane.getChildren().add(searchBox);
    StackPane.setAlignment(searchBox, Pos.TOP_LEFT);
    StackPane.setMargin(searchBox, new Insets(10, 0, 0, 10));
    }
    private void setupTextField() {
    searchBox = new TextField();
    searchBox.setMaxWidth(400);
    searchBox.setPromptText("Search for an address");
    }
    private void createLocatorTaskAndDefaultParameters() {
    locatorTask = new LocatorTask("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer");
    geocodeParameters = new GeocodeParameters();
    geocodeParameters.getResultAttributeNames().add("*");
    geocodeParameters.setMaxResults(1);
    geocodeParameters.setOutputSpatialReference(mapView.getSpatialReference());
    }
    private void performGeocode(String address) {
    ListenableFuture<List<GeocodeResult>> geocodeResults = locatorTask.geocodeAsync(address, geocodeParameters);
    geocodeResults.addDoneListener(() -> {
    try {
    List<GeocodeResult> geocodes = geocodeResults.get();
    if (geocodes.size() > 0) {
    GeocodeResult result = geocodes.get(0);
    } else {
    new Alert(Alert.AlertType.INFORMATION, "No results found.").show();
    }
    } catch (InterruptedException | ExecutionException e) {
    new Alert(Alert.AlertType.ERROR, "Error getting result.").show();
    e.printStackTrace();
    }
    });
    }
    private void displayResult(GeocodeResult geocodeResult) {
    graphicsOverlay.getGraphics().clear(); // clears the overlay of any previous result
    // create a graphic to display the address text
    String label = geocodeResult.getLabel();
    TextSymbol textSymbol = new TextSymbol(18, label, Color.BLACK, TextSymbol.HorizontalAlignment.CENTER, TextSymbol.VerticalAlignment.BOTTOM);
    Graphic textGraphic = new Graphic(geocodeResult.getDisplayLocation(), textSymbol);
    graphicsOverlay.getGraphics().add(textGraphic);
    // create a graphic to display the location as a red square
    SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.SQUARE, Color.RED, 12.0f);
    Graphic markerGraphic = new Graphic(geocodeResult.getDisplayLocation(), geocodeResult.getAttributes(), markerSymbol);
    graphicsOverlay.getGraphics().add(markerGraphic);
    mapView.setViewpointCenterAsync(geocodeResult.getDisplayLocation());
    }
    12 collapsed lines
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    }
  3. In the performGeocode() method, in the lambda expression body, call the displayResult() method, passing in result as the parameter. This will display the result of the geocode operation on the map.

    App.java
    118 collapsed lines
    // Copyright 2020 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    package com.example.app;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.paint.Color;
    import javafx.scene.control.Alert;
    import javafx.scene.control.TextField;
    import com.esri.arcgisruntime.concurrent.ListenableFuture;
    import com.esri.arcgisruntime.mapping.view.Graphic;
    import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
    import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
    import com.esri.arcgisruntime.symbology.TextSymbol;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
    import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    public class App extends Application {
    private MapView mapView;
    private GeocodeParameters geocodeParameters;
    private GraphicsOverlay graphicsOverlay;
    private LocatorTask locatorTask;
    private TextField searchBox;
    public static void main(String[] args) {
    Application.launch(args);
    }
    @Override
    public void start(Stage stage) {
    // set the title and size of the stage and show it
    stage.setTitle("Search for an address tutorial");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create a JavaFX scene with a stack pane as the root node
    // and add it to the scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    ArcGISRuntimeEnvironment.setApiKey("YOUR_ACCESS_TOKEN");
    // create a map view to display the map and add it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 144447.638572));
    // create a graphics overlay and add it to the map view
    graphicsOverlay = new GraphicsOverlay();
    mapView.getGraphicsOverlays().add(graphicsOverlay);
    setupTextField();
    createLocatorTaskAndDefaultParameters();
    stackPane.getChildren().add(searchBox);
    StackPane.setAlignment(searchBox, Pos.TOP_LEFT);
    StackPane.setMargin(searchBox, new Insets(10, 0, 0, 10));
    }
    private void setupTextField() {
    searchBox = new TextField();
    searchBox.setMaxWidth(400);
    searchBox.setPromptText("Search for an address");
    }
    private void createLocatorTaskAndDefaultParameters() {
    locatorTask = new LocatorTask("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer");
    geocodeParameters = new GeocodeParameters();
    geocodeParameters.getResultAttributeNames().add("*");
    geocodeParameters.setMaxResults(1);
    geocodeParameters.setOutputSpatialReference(mapView.getSpatialReference());
    }
    private void performGeocode(String address) {
    ListenableFuture<List<GeocodeResult>> geocodeResults = locatorTask.geocodeAsync(address, geocodeParameters);
    geocodeResults.addDoneListener(() -> {
    try {
    List<GeocodeResult> geocodes = geocodeResults.get();
    if (geocodes.size() > 0) {
    GeocodeResult result = geocodes.get(0);
    displayResult(result);
    39 collapsed lines
    } else {
    new Alert(Alert.AlertType.INFORMATION, "No results found.").show();
    }
    } catch (InterruptedException | ExecutionException e) {
    new Alert(Alert.AlertType.ERROR, "Error getting result.").show();
    e.printStackTrace();
    }
    });
    }
    private void displayResult(GeocodeResult geocodeResult) {
    graphicsOverlay.getGraphics().clear(); // clears the overlay of any previous result
    // create a graphic to display the address text
    String label = geocodeResult.getLabel();
    TextSymbol textSymbol = new TextSymbol(18, label, Color.BLACK, TextSymbol.HorizontalAlignment.CENTER, TextSymbol.VerticalAlignment.BOTTOM);
    Graphic textGraphic = new Graphic(geocodeResult.getDisplayLocation(), textSymbol);
    graphicsOverlay.getGraphics().add(textGraphic);
    // create a graphic to display the location as a red square
    SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.SQUARE, Color.RED, 12.0f);
    Graphic markerGraphic = new Graphic(geocodeResult.getDisplayLocation(), geocodeResult.getAttributes(), markerSymbol);
    graphicsOverlay.getGraphics().add(markerGraphic);
    mapView.setViewpointCenterAsync(geocodeResult.getDisplayLocation());
    }
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    }
  4. In the start() method, set an action on the searchBox to get the input text. Get the text from the search box and call performGeocode(), passing in the text as the parameter.

    App.java
    92 collapsed lines
    // Copyright 2020 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    package com.example.app;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.paint.Color;
    import javafx.scene.control.Alert;
    import javafx.scene.control.TextField;
    import com.esri.arcgisruntime.concurrent.ListenableFuture;
    import com.esri.arcgisruntime.mapping.view.Graphic;
    import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
    import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
    import com.esri.arcgisruntime.symbology.TextSymbol;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
    import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
    import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    public class App extends Application {
    private MapView mapView;
    private GeocodeParameters geocodeParameters;
    private GraphicsOverlay graphicsOverlay;
    private LocatorTask locatorTask;
    private TextField searchBox;
    public static void main(String[] args) {
    Application.launch(args);
    }
    @Override
    public void start(Stage stage) {
    // set the title and size of the stage and show it
    stage.setTitle("Search for an address tutorial");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create a JavaFX scene with a stack pane as the root node
    // and add it to the scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    ArcGISRuntimeEnvironment.setApiKey("YOUR_ACCESS_TOKEN");
    // create a map view to display the map and add it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 144447.638572));
    // create a graphics overlay and add it to the map view
    graphicsOverlay = new GraphicsOverlay();
    mapView.getGraphicsOverlays().add(graphicsOverlay);
    setupTextField();
    createLocatorTaskAndDefaultParameters();
    searchBox.setOnAction(event -> {
    String address = searchBox.getText();
    if (!address.isBlank()) {
    performGeocode(address);
    }
    });
    71 collapsed lines
    stackPane.getChildren().add(searchBox);
    StackPane.setAlignment(searchBox, Pos.TOP_LEFT);
    StackPane.setMargin(searchBox, new Insets(10, 0, 0, 10));
    }
    private void setupTextField() {
    searchBox = new TextField();
    searchBox.setMaxWidth(400);
    searchBox.setPromptText("Search for an address");
    }
    private void createLocatorTaskAndDefaultParameters() {
    locatorTask = new LocatorTask("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer");
    geocodeParameters = new GeocodeParameters();
    geocodeParameters.getResultAttributeNames().add("*");
    geocodeParameters.setMaxResults(1);
    geocodeParameters.setOutputSpatialReference(mapView.getSpatialReference());
    }
    private void performGeocode(String address) {
    ListenableFuture<List<GeocodeResult>> geocodeResults = locatorTask.geocodeAsync(address, geocodeParameters);
    geocodeResults.addDoneListener(() -> {
    try {
    List<GeocodeResult> geocodes = geocodeResults.get();
    if (geocodes.size() > 0) {
    GeocodeResult result = geocodes.get(0);
    displayResult(result);
    } else {
    new Alert(Alert.AlertType.INFORMATION, "No results found.").show();
    }
    } catch (InterruptedException | ExecutionException e) {
    new Alert(Alert.AlertType.ERROR, "Error getting result.").show();
    e.printStackTrace();
    }
    });
    }
    private void displayResult(GeocodeResult geocodeResult) {
    graphicsOverlay.getGraphics().clear(); // clears the overlay of any previous result
    // create a graphic to display the address text
    String label = geocodeResult.getLabel();
    TextSymbol textSymbol = new TextSymbol(18, label, Color.BLACK, TextSymbol.HorizontalAlignment.CENTER, TextSymbol.VerticalAlignment.BOTTOM);
    Graphic textGraphic = new Graphic(geocodeResult.getDisplayLocation(), textSymbol);
    graphicsOverlay.getGraphics().add(textGraphic);
    // create a graphic to display the location as a red square
    SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.SQUARE, Color.RED, 12.0f);
    Graphic markerGraphic = new Graphic(geocodeResult.getDisplayLocation(), geocodeResult.getAttributes(), markerSymbol);
    graphicsOverlay.getGraphics().add(markerGraphic);
    mapView.setViewpointCenterAsync(geocodeResult.getDisplayLocation());
    }
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    }
  5. Run the app. Ensure to run the app as a Gradle task and not as an application in your IDE. In the Gradle tool window, under Tasks > application, double-click run.

You should see a search box on the top left of the map. Search for an address by entering an address and press Return on the keyboard. The result of the search should display on the map as a red square.

What’s next?

Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials: