A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.arcgis.com/java/maps-2d/tutorials/display-a-map/ below:

Display a map | ArcGIS Maps SDK for Java

Learn how to create and display a map with a basemap layer.

A map contains layers of geographic data. A map contains a basemap layer and, optionally, one or more data layers. You can display a specific area of a map by using a map view and setting the location and zoom level.

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

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

Mapping and location services guide

For more background information about the topics in this tutorial, visit Maps (2D) and Basemaps.

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.

Note

This tutorial uses IntelliJ IDEA, but the code described will work within any supported Java IDE.

Steps Create a new Java project with Gradle
  1. Open IntelliJ IDEA.

  2. 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.)

  3. In the New Project window, do the following:

    Note

    If you wish to use an alternative to Gradle, see Install and Set Up for options.

  4. 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 load the gradle changes after modifying build.gradle.

    To load the Gradle changes, in the Gradle window, click the Reload All Gradle Projects icon in the upper left corner.

    build.gradle

    Use dark colors for code blocks Copy

    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
    plugins {
        id 'application'
        id 'org.openjfx.javafxplugin' version '0.1.0'
        id 'idea'
    }
    
    idea {
        module {
            downloadJavadoc = true
        }
    }
    
    ext {
        arcgisVersion = '200.6.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"
        implementation 'org.slf4j:slf4j-nop:2.0.16'
    }
    
    javafx {
        version = "21.0.5"
        modules = [ 'javafx.controls', 'javafx.graphics', 'javafx.fxml', 'javafx.web', 'javafx.media' ]
    }
    
    application {
        mainModule = "com.example.app"
        mainClass = "com.example.app.App"
    }
    
    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
    }
    
    wrapper {
        gradleVersion = '8.10.2'
    }
  5. 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.

  6. In the Project tool window, under your package com.example.app, right-click Main and click Refactor > Rename....

  7. Rename the Java class to App and click Refactor.

Add a UI for the map view

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

  1. In App.java, define a class named App that extends the JavaFX Application class.

  2. Initialize your member variable, mapView, and add it to the JavaFX UI.

    App.java

    Expand

    Use dark colors for code blocks

    45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 46 47 48 49 50 50 50 51 52 53 53 52 51 50 49 48 47 46 46 46 45 44 43 42 41 40 39 38 37 36 36

    Add line. Add line. Add line.
    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
    75
    76
        // 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);
    
Add a map

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

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

    App.java

    Expand

    Use dark colors for code blocks

    52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 51 50 51 52 53 54 55 56 56 56 56 56 55 55 55 54 53 52 51 50 49 48 47 46 45 45

    Add line.
    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
    75
    76
        // 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);
    
    
  2. To display the map in the map view, call the MapView.setMap() method, passing the newly created ArcGISMap as a parameter.

    App.java

    Expand

    Use dark colors for code blocks

    56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 55 54 54 54 54 54 55 56 57 58 59 59 58 58 58 57 56 55 54 53 52 51 50 49 48 48

    Add line. Add line.
    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
    75
    76
        ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    
        // set the map on the map view
        mapView.setMap(map);
    
    
  3. Center the map view at a specific point and scale on the Earth by setting a Viewpoint(double,double,double) on the mapView.

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

    The Viewpoint(double,double,double) constructor used in this tutorial takes a scale parameter. The scale value 144447.638572 is converted from zoom level 12. Zoom levels are often used as a shorthand for predetermined scale values in Web Mercator maps. Learn more in Zoom levels and scale.

    App.java

    Expand

    Use dark colors for code blocks

    56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 55 54 54 54 54 54 55 56 57 58 59 60 60 60 60 59 58 57 56 55 54 53 52 51 50 50

    Add line.
    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
    75
    76
        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));
    
Get an access token

You need an access token to use the location services used in this tutorial.

  1. Go to the Create an API key tutorial to obtain an access token using your ArcGIS Location Platform or ArcGIS Online account.

  2. Ensure that the following privilege 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

Set the API key property on the ArcGISRuntimeEnvironment. 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.

Note

Be sure to set the API Key before the mapping code, since the map needs the key to access the basemap.

App.java

Expand

Use dark colors for code blocks

37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 50 49 48 47 46 45 44 43 42 41 41

Add line.
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
75
76
  @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");
Warning

The access token is stored directly in the code as a convenience for this tutorial. Storing access tokens in the source code is not best practice.

Release API resources

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

App.java

Expand

Use dark colors for code blocks

65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 66 67 68 69 70 71 72 73 74 74 74

Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.
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
75
76
  /**
   * Stops and releases all resources used in application.
   */
  @Override
  public void stop() {
    if (mapView != null) {
      mapView.dispose();
    }
  }
Modularize the app

A Java module adds a higher level of aggregation above packages. A module must provide a module descriptor that specifies the dependencies, the packages the module makes available to other modules, and more.

You will create the module descriptor for this project in a file named module-info.java.

  1. In the Project tool window, under src/main, right-click the java folder, and click New > module-info.java.

  2. Inside module-info.java replace the module name (i.e. highlighted text) with com.example.app.

  3. In the body of the module descriptor, define the three required packages this application depends on: com.esri.arcgisruntime, javafx.graphics, and org.slf4j.nop.

  4. Export this project's module package to make it accessible to code in all other modules.

module-info.java

Expand

Use dark colors for code blocks

13 13 13 13 13 13 13 13 13 13 13 13 13 14 15 16 17 18 19 20 21 22 23 24 25

Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.
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
module com.example.app {
  // require ArcGIS Runtime module
  requires com.esri.arcgisruntime;

  // requires JavaFX modules that the application uses
  requires javafx.graphics;

  // requires SLF4j module
  requires org.slf4j.nop;

  exports com.example.app;
}
Run the app

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 map with the topographic basemap layer centered on the Santa Monica Mountains in California. Click, drag, and scroll the mouse wheel on the map view to explore the map.

What's next?

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4