Skip To Content ArcGIS for Developers Sign In Dashboard

Overview

You will learn: how to build a simple app that displays a 2D map.

With the ArcGIS Runtime SDK for Java, it's easy to create a simple starter app that displays a basemap. You can choose from a wide variety of raster and vector tile ArcGIS Online basemaps to build different types of applications. Most of the basemaps can be referenced with a static method such as createTopographicVector() or a basemap type, such as TOPOGRAPHIC_VECTOR. In order to set the location of the map, you can set the map view to a certain location on earth by specifying the latitude, longitude, and scale.

In this tutorial you will use the ArcGIS Java SDK to build a simple mapping app that displays a basemap and zooms to the Santa Monica Mountains in California.

Before you begin

Make sure your development environment meets the system requirements and you have the required Java dependencies installed. The tutorials are IDE agnostic and use Gradle, but if you would like to use a different development tool, visit the guide.

Steps

Create a starter app

  1. We prepared a project template for you to start with which sets up the required Java and Gradle project. Clone or download the Github project and unzip the starting template in to a new or empty folder.

  2. Run the Gradle build task. The plugins identified in the build.gradle settings will automatically fetch the required libraries. To run the task in a terminal, run the following command from the project root:

    ./gradlew build
    
    .\gradlew.bat build
    
  3. Open the App.java file in the project's src/main/java/com/mycompany/app source directory. It should contain the following code:

    package com.mycompany.app;
    
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.Basemap;
    import com.esri.arcgisruntime.mapping.view.MapView;
    
    public class App extends Application {
    
        private MapView mapView;
    
        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("My Map App");
            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);
    
            // create a MapView to display the map and add it to the stack pane
            mapView = new MapView();
            stackPane.getChildren().add(mapView);
    
            // create an ArcGISMap with the default imagery basemap
            ArcGISMap map = new ArcGISMap(Basemap.createImagery());
    
            // display the map by setting the map on the map view
            mapView.setMap(map);
        }
    
        /**
         * Stops and releases all resources used in application.
         */
        @Override
        public void stop() {
            if (mapView != null) {
                mapView.dispose();
            }
        }
    }
    
  4. Run your project now using the run task instead of the build task. Verify the app opens and shows window titled "My Map App" with a zoomed-out map inside it.

    ./gradlew run
    
    .\gradlew.bat run
    

Add a map to your app

  1. Add a new private method named setupMap. This method will initialize the map with a basemap and the designated area of interest and add the map to the map view. This method will replace the prior code that set the map.

       private void setupMap() {
           if (mapView != null) {
               Basemap.Type basemapType = Basemap.Type.STREETS_VECTOR;
               double latitude = 34.02700;
               double longitude = -118.80543;
               int levelOfDetail = 12;
               ArcGISMap map = new ArcGISMap(basemapType, latitude, longitude, levelOfDetail);
               mapView.setMap(map);
           }
       }
    
  2. Remove the existing code that set up the map. Replace it with the call to the new setupMap method that you created above.

             mapView = new MapView();
             stackPane.getChildren().add(mapView);
    
             /* *** REMOVE *** */
             // create an ArcGISMap with the default imagery basemap
             ArcGISMap map = new ArcGISMap(Basemap.createImagery());
    
             // display the map by setting the map on the map view
             mapView.setMap(map);
    
             mapView = new MapView();
             stackPane.getChildren().add(mapView);
    
             /* *** ADD *** */
             setupMap();
    
  3. Build and run your code to view the map.

    ./gradlew run
    
    .\gradlew.bat run
    

Congratulations, you're done!

Your app should run and display a map focused on the Santa Monica Mountains in California. Compare your solution with our completed solution project. We will refer back to this project as the starting project for many of the Java tutorials.

Challenge

Explore basemaps

Visit the documentation to learn more about the different types of basemaps available, such as LIGHT_GRAY_CANVAS_VECTOR or STREETS_WITH_RELIEF_VECTOR. Experiment with a few of the many options before choosing your favorite for your project.

Change view

Try the other ArcGISMap constructors to set a different initial view by changing the latitude, longitude, and level of detail.

Your own custom basemap

Learn how to create and use your own custom basemaps with the Style a vector basemap and Display a styled vector basemap tutorials.