Labeling features is useful to visually display information or attributes on a scene. For example, city officials or maintenance crews may want to show installation dates of features of a gas network.
How to use the sample
Pan and zoom to explore the scene. Notice the labels showing installation dates of features in the 3D gas network.
How it works
Create an ArcGISScene using a PortalItem.
Add the scene to a SceneView and load it.
After loading is complete, obtain the FeatureLayer from the scene's operational layers.
Set the feature layer's setLabelsEnabled property to true.
Create a TextSymbol to use for displaying the label text.
Create a LabelDefinition using an ArcadeLabelExpression and the text symbol.
Add the definition to the feature layer's labelDefinitions list.
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright 2021 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.esri.samples.show_labels_on_layer_in_3d;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import com.esri.arcgisruntime.arcgisservices.LabelingPlacement;
import com.esri.arcgisruntime.arcgisservices.LabelDefinition;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISScene;
import com.esri.arcgisruntime.mapping.labeling.ArcadeLabelExpression;
import com.esri.arcgisruntime.mapping.view.SceneView;
import com.esri.arcgisruntime.portal.Portal;
import com.esri.arcgisruntime.portal.PortalItem;
import com.esri.arcgisruntime.symbology.TextSymbol;
publicclassShowLabelsOnLayerIn3dSampleextendsApplication{
private SceneView sceneView;
@Overridepublicvoidstart(Stage stage){
try {
// create stack pane and JavaFX app scene StackPane stackPane = new StackPane();
Scene fxScene = new Scene(stackPane);
// set title, size, and add the JavaFX scene to stage stage.setTitle("Show Labels on Layer in 3D");
stage.setWidth(800);
stage.setHeight(700);
stage.setScene(fxScene);
stage.show();
// create a scene from an ArcGIS Online portal itemvar portalItem = new PortalItem(
new Portal("https://www.arcgis.com", false), "850dfee7d30f4d9da0ebca34a533c169");
ArcGISScene scene = new ArcGISScene(portalItem);
// set the scene to the sceneview, and add the sceneview to the JavaFX stack pane sceneView = new SceneView();
sceneView.setArcGISScene(scene);
// create a progress indicator and display it to indicate loading is in progressvar progressIndicator = new ProgressIndicator();
progressIndicator.setVisible(true);
stackPane.getChildren().addAll(sceneView, progressIndicator);
scene.addDoneLoadingListener(() -> {
if (scene.getLoadStatus() == LoadStatus.LOADED) {
// remove the progress indicator when the scene loads progressIndicator.setVisible(false);
// filter through the scene's operational layers to find the layer named "Gas",// and find the "Gas Main" sublayer to apply the label definition to scene.getOperationalLayers().stream()
.filter(layer -> layer.getName().equals("Gas"))
.flatMap(gasLayer -> gasLayer.getSubLayerContents().stream())
.filter(subLayer -> subLayer.getName().equals("Gas Main"))
.findFirst()
.ifPresent(gasMainSubLayer -> {
FeatureLayer featureLayer = (FeatureLayer) gasMainSubLayer;
// enable the labelling for the feature layer featureLayer.setLabelsEnabled(true);
// clear any existing label definitions on the layer featureLayer.getLabelDefinitions().clear();
// create a text symbol to set to the label definitionvar textSymbol = new TextSymbol();
textSymbol.setColor(Color.ORANGE);
textSymbol.setHaloColor(Color.WHITE);
textSymbol.setHaloWidth(2);
textSymbol.setSize(16);
// create a label definition with an Arcade expression scriptvar arcadeLabelExpression = new ArcadeLabelExpression("Text($feature.INSTALLATIONDATE, `DD MMM YY`)");
var labelDefinition = new LabelDefinition(arcadeLabelExpression, textSymbol);
labelDefinition.setPlacement(LabelingPlacement.LINE_ABOVE_ALONG);
labelDefinition.setUseCodedValues(true);
// add the label definition to the feature layer featureLayer.getLabelDefinitions().add(labelDefinition);
});
} else {
new Alert(Alert.AlertType.ERROR, "Scene failed to load " + scene.getLoadError().getCause()).show();
}
});
} catch (Exception e) {
// on any error, display the stack trace. e.printStackTrace();
}
}
/**
* Stops and releases all resources used in application.
*/@Overridepublicvoidstop(){
if (sceneView != null) {
sceneView.dispose();
}
}
/**
* Opens and runs application.
*
* @param args arguments passed to this application
*/publicstaticvoidmain(String[] args){
Application.launch(args);
}
}