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.
Prerequisites
The following are required for this tutorial:
An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
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.
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.
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.
You can also run Gradle tasks via the command line. Consult Gradle's documentation to learn how this is done.
In the Project tool window, under src/main, right-click the java folder, and click New > Package.
Name the package com.example.app.
Right-click this package and click New > Java Class.
Name the class App.
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.
In App.Java, create a new public class named App that extends the JavaFX Application class.
The mapView member variable allows you to easily reference your MapView from other parts of the application.
Create the main() method, where you call Application.launch(args).
This code calls the static method launch() of the JavaFX class Application, which creates an instance of your App class on the JavaFX Application Thread and then calls the start() method. For a description of the JavaFX lifecycle, see Application.
Override the start() method, in which you configure the JavaFX Stage with a title and dimensions, and then show it.
Note that the start method is abstract and must be overridden. It takes a single parameter of the JavaFX type Stage.
Create a JavaFX StackPane, and use it to create a JavaFX Scene. Then set the scene on the stage.
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.
An API key is required to enable access to services, web maps, and web scenes hosted in ArcGIS Online.
If you haven't already, go to your developer dashboard to get your API key.
For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.
Before running the ArcGIS Java API code, set the API key property on the ArcGISRuntimeEnvironment with your API key. In the code below, replace YOUR_API_KEY with your actual API Key. Be sure to surround your API Key with quotes, because this value is a string.
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
77
@Overridepublicvoidstart(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);
// Note: it is not best practice to store API keys in source code.// The API key is referenced here for the convenience of this tutorial. String yourApiKey = "YOUR_API_KEY";
ArcGISRuntimeEnvironment.setApiKey(yourApiKey);
Expand
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 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.
Stop the app
To ensure that API resources used in the application are released when it is closed, override the JavaFX stop method and call dispose() on the mapView: