Skip to content

Commit d2e22f5

Browse files
authored
Merge pull request #243 from Esri/display-grid
display grid sample
2 parents a5a7b53 + 0f6f063 commit d2e22f5

File tree

6 files changed

+311
-1
lines changed

6 files changed

+311
-1
lines changed
537 KB
Loading
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright 2018 Esri.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.esri.samples.displayinformation.display_grid;
18+
19+
import javafx.beans.binding.Bindings;
20+
import javafx.fxml.FXML;
21+
import javafx.scene.control.CheckBox;
22+
import javafx.scene.control.ColorPicker;
23+
import javafx.scene.control.ComboBox;
24+
import javafx.scene.paint.Color;
25+
26+
import com.esri.arcgisruntime.geometry.Point;
27+
import com.esri.arcgisruntime.geometry.SpatialReference;
28+
import com.esri.arcgisruntime.mapping.ArcGISMap;
29+
import com.esri.arcgisruntime.mapping.Basemap;
30+
import com.esri.arcgisruntime.mapping.Viewpoint;
31+
import com.esri.arcgisruntime.mapping.view.Grid;
32+
import com.esri.arcgisruntime.mapping.view.LatitudeLongitudeGrid;
33+
import com.esri.arcgisruntime.mapping.view.MapView;
34+
import com.esri.arcgisruntime.mapping.view.MgrsGrid;
35+
import com.esri.arcgisruntime.mapping.view.UsngGrid;
36+
import com.esri.arcgisruntime.mapping.view.UtmGrid;
37+
import com.esri.arcgisruntime.symbology.ColorUtil;
38+
import com.esri.arcgisruntime.symbology.LineSymbol;
39+
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
40+
import com.esri.arcgisruntime.symbology.TextSymbol;
41+
42+
public class DisplayGridController {
43+
44+
@FXML private MapView mapView;
45+
@FXML private ComboBox<GridType> gridTypeComboBox;
46+
@FXML private CheckBox labelsVisibleCheckBox;
47+
@FXML private CheckBox gridVisibleCheckBox;
48+
@FXML private ColorPicker gridColorPicker;
49+
@FXML private ColorPicker labelColorPicker;
50+
@FXML private ComboBox<Grid.LabelPosition> labelPositionComboBox;
51+
@FXML private ComboBox<LatitudeLongitudeGrid.LabelFormat> labelFormatComboBox;
52+
53+
/**
54+
* Used for combo box.
55+
*/
56+
private enum GridType {
57+
LAT_LON, UTM, USNG, MGRS
58+
}
59+
60+
public void initialize() {
61+
ArcGISMap map = new ArcGISMap(Basemap.createImagery());
62+
map.setInitialViewpoint(new Viewpoint(new Point(-10336141.70018318, 5418213.05332071, SpatialReference.create
63+
(3857)), 6450785));
64+
mapView.setMap(map);
65+
66+
// set initial values for options
67+
gridTypeComboBox.getItems().addAll(GridType.values());
68+
gridTypeComboBox.setValue(GridType.LAT_LON);
69+
gridColorPicker.setValue(Color.WHITE);
70+
labelColorPicker.setValue(Color.RED);
71+
labelPositionComboBox.getItems().addAll(Grid.LabelPosition.values());
72+
labelPositionComboBox.setValue(Grid.LabelPosition.TOP_LEFT);
73+
labelFormatComboBox.getItems().addAll(LatitudeLongitudeGrid.LabelFormat.values());
74+
labelFormatComboBox.setValue(LatitudeLongitudeGrid.LabelFormat.DECIMAL_DEGREES);
75+
76+
// label position and format only apply to Lat Lon grid type
77+
labelPositionComboBox.disableProperty().bind(Bindings.createBooleanBinding(() ->
78+
gridTypeComboBox.getSelectionModel().getSelectedItem() != GridType.LAT_LON,
79+
gridTypeComboBox.getSelectionModel().selectedItemProperty())
80+
);
81+
labelFormatComboBox.disableProperty().bind(Bindings.createBooleanBinding(() ->
82+
gridTypeComboBox.getSelectionModel().getSelectedItem() != GridType.LAT_LON,
83+
gridTypeComboBox.getSelectionModel().selectedItemProperty())
84+
);
85+
86+
// update the grid with the default values on start
87+
updateGrid();
88+
}
89+
90+
/**
91+
* Updates the map view's grid when the "Update" button is clicked.
92+
*/
93+
@FXML
94+
private void updateGrid() {
95+
// grid type
96+
Grid grid = null;
97+
switch (gridTypeComboBox.getSelectionModel().getSelectedItem()) {
98+
case LAT_LON:
99+
grid = new LatitudeLongitudeGrid();
100+
break;
101+
case UTM:
102+
grid = new UtmGrid();
103+
break;
104+
case USNG:
105+
grid = new UsngGrid();
106+
break;
107+
case MGRS:
108+
grid = new MgrsGrid();
109+
break;
110+
}
111+
112+
// color the grid lines and labels for each grid level
113+
for (int i = 0; i < grid.getLevelCount(); i++) {
114+
// grid lines
115+
LineSymbol gridLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, ColorUtil.colorToArgb(gridColorPicker
116+
.getValue()), 1 + i);
117+
grid.setLineSymbol(i, gridLineSymbol);
118+
119+
// labels
120+
TextSymbol labelTextSymbol = new TextSymbol(14, "text", ColorUtil.colorToArgb(labelColorPicker.getValue()),
121+
TextSymbol.HorizontalAlignment.LEFT, TextSymbol.VerticalAlignment.BOTTOM);
122+
labelTextSymbol.setHaloColor(0xFFFFFFFF); //white
123+
labelTextSymbol.setHaloWidth(2 + i);
124+
grid.setTextSymbol(i, labelTextSymbol);
125+
}
126+
127+
// grid visibility
128+
grid.setVisible(gridVisibleCheckBox.isSelected());
129+
130+
// label visibility
131+
grid.setLabelVisible(labelsVisibleCheckBox.isSelected());
132+
133+
// label position and format
134+
if (grid instanceof LatitudeLongitudeGrid) {
135+
grid.setLabelPosition(labelPositionComboBox.getSelectionModel().getSelectedItem());
136+
((LatitudeLongitudeGrid) grid).setLabelFormat(labelFormatComboBox.getSelectionModel().getSelectedItem());
137+
}
138+
139+
// set the grid
140+
mapView.setGrid(grid);
141+
}
142+
143+
/**
144+
* Disposes of application resources.
145+
*/
146+
void terminate() {
147+
148+
if (mapView != null) {
149+
mapView.dispose();
150+
}
151+
}
152+
153+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2018 Esri.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.esri.samples.displayinformation.display_grid;
18+
19+
import java.io.IOException;
20+
21+
import javafx.application.Application;
22+
import javafx.fxml.FXMLLoader;
23+
import javafx.scene.Parent;
24+
import javafx.scene.Scene;
25+
import javafx.stage.Stage;
26+
27+
public class DisplayGridSample extends Application {
28+
29+
private static DisplayGridController controller;
30+
31+
@Override
32+
public void start(Stage stage) throws IOException {
33+
// set up the scene
34+
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/display_grid.fxml"));
35+
Parent root = loader.load();
36+
controller = loader.getController();
37+
Scene scene = new Scene(root);
38+
39+
// set up the stage
40+
stage.setTitle("Display Grid Sample");
41+
stage.setWidth(800);
42+
stage.setHeight(700);
43+
stage.setScene(scene);
44+
stage.show();
45+
}
46+
47+
/**
48+
* Stops and releases all resources used in application.
49+
*/
50+
@Override
51+
public void stop() {
52+
controller.terminate();
53+
}
54+
55+
/**
56+
* Opens and runs application.
57+
*
58+
* @param args arguments passed to this application
59+
*/
60+
public static void main(String[] args) {
61+
62+
Application.launch(args);
63+
}
64+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<h1>Display Grid</h1>
2+
3+
<p>Demonstrates how to display and style different types of grids over a map.</p>
4+
5+
<p><img src="DisplayGrid.png"/></p>
6+
7+
<h2>How to use the sample</h2>
8+
9+
<p>Select the different grid style options and click "Update" to set the grid.</p>
10+
11+
<h2>How it works</h2>
12+
13+
<p>To show and style a grid:</p>
14+
15+
<ol>
16+
<li>Create an instance of one of the <code>Grid</code> types.</li>
17+
<li>Grid lines and labels can be styled per grid level with <code>grid.setLineSymbol(gridLevel, lineSymbol)</code>
18+
and <code>grid.setTextSymbol(gridLevel, textSymbol)</code>.</li>
19+
<li>The label position can be set with <code>grid.setLabelPosition(labelPosition)</code>.</li>
20+
<li>For the <code>LatitudeLongitudeGrid</code> type, you can specify a label format of <code>DECIMAL_DEGREES</code>
21+
or <code>DEGREES_MINUTES_SECONDS</code>.</li>
22+
<li>To set the grid, use <code>mapView.setGrid(grid)</code>.</li>
23+
</ol>
24+
25+
<h2>Features</h2>
26+
27+
<ul>
28+
<li>Grid</li>
29+
<li>LatitudeLongitudeGrid</li>
30+
<li>LineSymbol</li>
31+
<li>MapView</li>
32+
<li>MgrsGrid</li>
33+
<li>SimpleLineSymbol</li>
34+
<li>TextSymbol</li>
35+
<li>UsngGrid</li>
36+
<li>UtmGrid</li>
37+
</ul>

src/main/resources/css/style.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@
2121

2222
.panel-region .check-box {
2323
-fx-text-fill: white;
24-
}
24+
}
25+
26+
.color-picker .color-picker-label {
27+
-fx-text-fill: black;
28+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2018 Esri.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License"); you may not
6+
~ use this file except in compliance with the License. You may obtain a copy of
7+
~ the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
~ License for the specific language governing permissions and limitations under
15+
~ the License.
16+
-->
17+
18+
<?import javafx.scene.control.Button?>
19+
<?import javafx.scene.control.CheckBox?>
20+
<?import javafx.scene.control.ColorPicker?>
21+
<?import javafx.scene.control.ComboBox?>
22+
<?import javafx.scene.control.Label?>
23+
<?import javafx.scene.layout.*?>
24+
<?import com.esri.arcgisruntime.mapping.view.MapView?>
25+
<StackPane fx:controller="com.esri.samples.displayinformation.display_grid.DisplayGridController" xmlns:fx="http://javafx.com/fxml"
26+
stylesheets="/css/style.css">
27+
<MapView fx:id="mapView"/>
28+
<VBox StackPane.alignment="TOP_LEFT" maxWidth="300" maxHeight="300" styleClass="panel-region" spacing="10"
29+
alignment="CENTER">
30+
<GridPane hgap="10" vgap="10">
31+
<columnConstraints>
32+
<ColumnConstraints percentWidth="50"/>
33+
<ColumnConstraints percentWidth="50"/>
34+
</columnConstraints>
35+
<Label text="Grid type" GridPane.rowIndex="0" GridPane.columnIndex="0"/>
36+
<ComboBox fx:id="gridTypeComboBox" GridPane.rowIndex="0" GridPane.columnIndex="1"/>
37+
<Label text="Labels visible" GridPane.rowIndex="1" GridPane.columnIndex="0"/>
38+
<CheckBox fx:id="labelsVisibleCheckBox" selected="true" GridPane.rowIndex="1" GridPane.columnIndex="1"/>
39+
<Label text="Grid visible" GridPane.rowIndex="2" GridPane.columnIndex="0"/>
40+
<CheckBox fx:id="gridVisibleCheckBox" selected="true" GridPane.rowIndex="2" GridPane.columnIndex="1"/>
41+
<Label text="Grid color" GridPane.rowIndex="3" GridPane.columnIndex="0"/>
42+
<ColorPicker fx:id="gridColorPicker" GridPane.rowIndex="3" GridPane.columnIndex="1"/>
43+
<Label text="Label color" GridPane.rowIndex="4" GridPane.columnIndex="0"/>
44+
<ColorPicker fx:id="labelColorPicker" GridPane.rowIndex="4" GridPane.columnIndex="1"/>
45+
<Label text="Label position" GridPane.rowIndex="5" GridPane.columnIndex="0"/>
46+
<ComboBox fx:id="labelPositionComboBox" GridPane.rowIndex="5" GridPane.columnIndex="1"/>
47+
<Label text="Label format" GridPane.rowIndex="6" GridPane.columnIndex="0"/>
48+
<ComboBox fx:id="labelFormatComboBox" GridPane.rowIndex="6" GridPane.columnIndex="1"/>
49+
</GridPane>
50+
<Button text="Update" onAction="#updateGrid"/>
51+
</VBox>
52+
</StackPane>

0 commit comments

Comments
 (0)