Display a map

Learn how to create and display a with a .

display a map

A map contains of geographic data. A map contains a and, optionally, one or more . You can display a specific area of a map by using a and setting the and .

In this tutorial, you create and display a of the Santa Monica Mountains in California using the topographic .

The map and code will be used as the starting point for other 2D tutorials.

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

Create a new Java project with Gradle

  1. Open IntelliJ IDEA.

    • From the Welcome to IntelliJ IDEA screen, click the New Project button. (If you're already inside a project, click File > New > Project in the menu bar.)
    • In the New Project window, select Gradle from the list on the left, make sure Java is checked under Additional Libraries and Frameworks, and click Next.
    • In the next window, enter a name for your new project and choose a location to save it.

    • Click Artifact Coordinates to expand the drop-down. In GroupId enter com.example.app. You can leave the defaults for ArtifactId and Version. Then click Finish.

  2. In the Project tool window, replace the contents of the build.gradle file with the following script to configure your app and reference the API. Make sure that you import the Gradle changes once you have replaced the contents.

    build.gradle
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    plugins {
        id 'application'
        id 'org.openjfx.javafxplugin' version '0.0.13'
    }
    
    ext {
        arcgisVersion = '100.15.0'
    }
    
    repositories {
        mavenCentral()
        maven {
            url 'https://esri.jfrog.io/artifactory/arcgis'
        }
    }
    
    configurations {
        natives
    }
    
    dependencies {
        implementation "com.esri.arcgisruntime:arcgis-java:$arcgisVersion"
        natives "com.esri.arcgisruntime:arcgis-java-jnilibs:$arcgisVersion"
        natives "com.esri.arcgisruntime:arcgis-java-resources:$arcgisVersion"
        runtimeOnly "org.slf4j:slf4j-nop:1.7.32"
    }
    
    javafx {
        version = "17.0.2"
        modules = [ 'javafx.controls' ]
    }
    
    task copyNatives(type: Copy) {
        description = "Copies the arcgis native libraries into the .arcgis directory for development."
        group = "build"
        configurations.natives.asFileTree.each {
            from(zipTree(it))
        }
        into "${System.properties.getProperty("user.home")}/.arcgis/$arcgisVersion"
    }
    
    run {
        dependsOn copyNatives
        mainClassName = 'com.example.app.App'
    }
  3. Click View > Tool Windows > Gradle to open the Gradle view, then in Tasks > build, double-click copyNatives. This unpacks the native library dependencies to $USER_HOME/.arcgis.

  4. In the Project tool window, under src/main, right-click the java folder, and click New > Package.

  5. Name the package com.example.app.

  6. Right-click this package and click New > Java Class.

  7. Name the class App.

Add a UI for the map view

A is a UI component that displays a . It also handles user interactions with the map. Use JavaFX to add a map view to the UI.

  1. In App.Java, create a new public class named App that extends the JavaFX Application class.

    • Add a member variable for a MapView.

    • Create the main() method, where you call Application.launch(args).

    • Override the start() method, in which you configure the JavaFX Stage with a title and dimensions, and then show it.

    • Create a JavaFX StackPane, and use it to create a JavaFX Scene. Then set the scene on the stage.

      App.java
      Expand
      28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      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("Display a map 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);
      
        }
      
      }
  2. Add the mapView to the UI.

    App.java
    Expand
    44 45 46 47 48 49 50 51
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
        // 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 map view to display the map and add it to the stack pane
        mapView = new MapView();
        stackPane.getChildren().add(mapView);
    
    Expand

Add a map

Use the to display a centered on the Santa Monica Mountains in California. The map will contain a topographic .

  1. Create a new ArcGISMap with a topographic basemap style.

    App.java
    Expand
    49 50 51 52 53 54
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
        // 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);
    
    
    Expand
  2. To display the map in the map view, call the mapView's setMap() method, passing the newly created ArcGISMap as a parameter.

    App.java
    Expand
    53 54 55 56 57
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
        ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    
        // set the map on the map view
        mapView.setMap(map);
    
    
    Expand
  3. Center the map view at a specific point and scale on the Earth by setting a Viewpoint on it.

    Provide latitude and longitude coordinates and a scale value as parameters to a new Viewpoint. Then set it on the mapView with setViewpoint().

    App.java
    Expand
    53 54 55 56 57 58
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
        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));
    
    Expand

Get an access token

You need an to use the used in this tutorial.

  1. Go to the Create an API key tutorial to obtain an using your or account.

  2. Ensure that the following is enabled: Location services > Basemaps > Basemap styles service.

  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.

Set your API key

  1. Before running the ArcGIS Java API code, set the API key property on the ArcGISRuntimeEnvironment with your . In the code below, 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
    Expand
    36 37 38 39 40 41 42 43 44 45 46 47 48 49
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
      @Override
      public void start(Stage stage) {
        // set the title and size of the stage and show it
        stage.setTitle("Display a map 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");
    
    Expand
  2. 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, click run.

You should see a with the topographic centered on the Santa Monica Mountains in California. Click, drag, and scroll the mouse wheel on the map view to explore the map.

Stop the app

To ensure that ArcGIS Runtime API resources used in the application are released when it is closed, override the JavaFX stop method and call dispose() on the mapView:

App.java
Expand
64 65 66 67 68 69 70 71 72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  /**
   * Stops and releases all resources used in application.
   */
  @Override
  public void stop() {
    if (mapView != null) {
      mapView.dispose();
    }
  }
Expand

What's next?

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

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.

You can no longer sign into this site. Go to your ArcGIS portal or the ArcGIS Location Platform dashboard to perform management tasks.

Your ArcGIS portal

Create, manage, and access API keys and OAuth 2.0 developer credentials, hosted layers, and data services.

Your ArcGIS Location Platform dashboard

Manage billing, monitor service usage, and access additional resources.

Learn more about these changes in the What's new in Esri Developers June 2024 blog post.

Close