Skip to content

Commit 602d325

Browse files
authored
Add draggable graphics to Update Graphics sample (#579)
* Add draggable graphic * Update readme, gif, screenshot * Add Esc key action * Remove unused boolean * Reorder point * Fix comments * Update README.md * Add changes from PR * Update README.md * Add back comments * Update README.md * Update how to use * Fix from PR * Fix image size
1 parent c0a2472 commit 602d325

File tree

4 files changed

+66
-71
lines changed

4 files changed

+66
-71
lines changed

display_information/update-graphics/README.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,20 @@ A field worker may want to update a graphic's properties to adjust which real-wo
1010

1111
## How to use the sample
1212

13-
Update Graphic's Location:
14-
- click on a graphic, then click the update location button
15-
- now click a new location on the map to move graphic
16-
17-
Update Graphic's Description:
18-
- click on a graphic, then click the update description button
19-
- enter a new description and click ok
20-
21-
Update Graphic's Symbol:
22-
- click on a graphic, then choose a symbol from the update symbol drop down box
13+
To update the graphic's location, click on a graphic and then drag it to a new location. Use the "Update Description" button to provide a new description for the graphic, and use the "Update Symbol" drop down menu to choose a new symbol. Click away from the graphic to de-select it.
2314

2415
## How it works
2516

2617
1. Capture clicks on the `MapView` by using `.setOnMouseClicked()`. Then use the event to create a `Point` from the clicked location.
2718
2. Identify the clicked graphics using `MapView.identifyGraphicsOverlayAsync(graphicsOverlay, pointClicked, tolerance, max results)`.
2819
3. To select a clicked graphics, iterate through the list of graphics returned by the identification method, and set each graphic's selection property to `true`.
29-
4. To update graphic's location, create a new `Point` from the user's clicked point, and use `Graphic.setGeometry(point)` to modify the graphic's geometry.
20+
4. To update a graphic's location, capture drags on the map view using `.setOnMouseDragged()`, and use `Graphic.setGeometry(point)` to modify the graphic's geometry from the dragged location.
3021
5. To update a graphic's attribute, get the attributes of the selected graphic and set the `DESCRIPTION` key with the desired string value using `Graphic.getAttributes().put("DESCRIPTION",)`.
3122
6. To update graphic's symbol simply assign that symbol to the selected graphic using `Graphic.setSymbol(SimpleMarkerSymbol)`.
3223

3324
## Additional information
3425

35-
A graphic's geometry is its location on a map. The symbol controls how a graphic will be displayed to a map. And the attributes store information about the graphic in key value pairs.
26+
A graphic's geometry is its location on a map. The symbol controls how a graphic will be displayed to a map. The attributes store information about the graphic in key value pairs.
3627

3728
## Relevant API
3829

17.8 KB
Loading
263 Bytes
Loading

display_information/update-graphics/src/main/java/com/esri/samples/update_graphics/UpdateGraphicsSample.java

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import javafx.geometry.Insets;
2121
import javafx.geometry.Point2D;
2222
import javafx.geometry.Pos;
23+
import javafx.scene.Cursor;
2324
import javafx.scene.Scene;
2425
import javafx.scene.control.Alert;
2526
import javafx.scene.control.Button;
@@ -41,6 +42,7 @@
4142
import com.esri.arcgisruntime.concurrent.ListenableFuture;
4243
import com.esri.arcgisruntime.geometry.Point;
4344
import com.esri.arcgisruntime.geometry.SpatialReference;
45+
import com.esri.arcgisruntime.geometry.SpatialReferences;
4446
import com.esri.arcgisruntime.mapping.ArcGISMap;
4547
import com.esri.arcgisruntime.mapping.Basemap;
4648
import com.esri.arcgisruntime.mapping.view.Graphic;
@@ -51,17 +53,14 @@
5153

5254
public class UpdateGraphicsSample extends Application {
5355

54-
private boolean isUpdateLocationActive;
5556
private List<SimpleMarkerSymbol> markers;
56-
private Button updateLocationButton;
5757
private Button updateDescriptionButton;
5858
private ComboBox<String> symbolBox;
5959

60-
private ArcGISMap map; // keep loadable in scope to avoid garbage collection
6160
private MapView mapView;
62-
private Graphic selectedGraphic;
61+
private Graphic identifiedGraphic;
6362
private GraphicsOverlay graphicsOverlay;
64-
private ListenableFuture<IdentifyGraphicsOverlayResult> identifyGraphics;
63+
private Point2D mapViewPoint;
6564

6665
// colors for symbols
6766
private static final int PURPLE = 0xFF800080;
@@ -90,18 +89,15 @@ public void start(Stage stage) {
9089
controlsVBox.setBackground(new Background(new BackgroundFill(Paint.valueOf("rgba(0,0,0,0.3)"), CornerRadii.EMPTY,
9190
Insets.EMPTY)));
9291
controlsVBox.setPadding(new Insets(10.0));
93-
controlsVBox.setMaxSize(180, 150);
92+
controlsVBox.setMaxSize(180, 100);
9493
controlsVBox.getStyleClass().add("panel-region");
9594

9695
// create buttons for user interaction
97-
updateLocationButton = new Button("Update Location");
9896
updateDescriptionButton = new Button("Update Description");
99-
updateLocationButton.setMaxWidth(Double.MAX_VALUE);
10097
updateDescriptionButton.setMaxWidth(Double.MAX_VALUE);
101-
updateLocationButton.setDisable(true);
10298
updateDescriptionButton.setDisable(true);
10399

104-
// create section for combo box
100+
// create combo box for the UI
105101
Label symbolLabel = new Label("Update Symbol:");
106102
symbolLabel.getStyleClass().add("panel-label");
107103
symbolBox = new ComboBox<>();
@@ -112,16 +108,16 @@ public void start(Stage stage) {
112108

113109
// set the symbol of the graphic
114110
symbolBox.showingProperty().addListener((obs, wasShowing, isShowing) -> {
115-
if (selectedGraphic.isSelected() && !isShowing) {
116-
selectedGraphic.setSymbol(markers.get(symbolBox.getSelectionModel().getSelectedIndex()));
111+
if (identifiedGraphic.isSelected() && !isShowing) {
112+
identifiedGraphic.setSymbol(markers.get(symbolBox.getSelectionModel().getSelectedIndex()));
117113
}
118114
});
119115

120-
// add label, dropdown and buttons to the control panel
121-
controlsVBox.getChildren().addAll(updateLocationButton, updateDescriptionButton, symbolLabel, symbolBox);
116+
// add the button, label, and combo box to the control panel
117+
controlsVBox.getChildren().addAll(updateDescriptionButton, symbolLabel, symbolBox);
122118

123119
// create a ArcGISMap with basemap light gray canvas
124-
map = new ArcGISMap(Basemap.Type.LIGHT_GRAY_CANVAS, 56.075844, -2.681572, 13);
120+
ArcGISMap map = new ArcGISMap(Basemap.Type.LIGHT_GRAY_CANVAS, 56.075844, -2.681572, 13);
125121

126122
// create a map view and set the map to it
127123
mapView = new MapView();
@@ -136,17 +132,10 @@ public void start(Stage stage) {
136132
// create default graphics for graphics overlay
137133
createGraphics();
138134

139-
// when clicked allow user to move graphic's location
140-
updateLocationButton.setOnAction(e -> {
141-
if (selectedGraphic.isSelected()) {
142-
isUpdateLocationActive = true;
143-
}
144-
});
145-
146135
updateDescriptionButton.setOnAction(e -> {
147-
if (selectedGraphic.isSelected()) {
136+
if (identifiedGraphic.isSelected()) {
148137
// get attributes from selected graphic
149-
java.util.Map<String, Object> attributes = selectedGraphic.getAttributes();
138+
java.util.Map<String, Object> attributes = identifiedGraphic.getAttributes();
150139

151140
// create input dialog
152141
TextInputDialog dialog = new TextInputDialog();
@@ -167,39 +156,51 @@ public void start(Stage stage) {
167156

168157
mapView.setOnMouseClicked(e -> {
169158
if (e.getButton() == MouseButton.PRIMARY && e.isStillSincePress()) {
159+
// set the cursor to default
160+
mapView.setCursor(Cursor.DEFAULT);
161+
170162
// clear any selected graphic
171163
graphicsOverlay.clearSelection();
172164

173-
// create a point from location clicked
174-
Point2D mapViewPoint = new Point2D(e.getX(), e.getY());
175-
176-
if (isUpdateLocationActive) {
177-
// add new location to selected graphic
178-
Point mapPoint = mapView.screenToLocation(mapViewPoint);
179-
selectedGraphic.setGeometry(mapPoint);
180-
isUpdateLocationActive = false;
181-
} else {
182-
// identify the graphic that was selected
183-
identifyGraphics = mapView.identifyGraphicsOverlayAsync(graphicsOverlay, mapViewPoint, 10, false);
184-
185-
identifyGraphics.addDoneListener(() -> {
186-
try {
187-
if (!identifyGraphics.get().getGraphics().isEmpty()) {
188-
// store the selected graphic
189-
selectedGraphic = identifyGraphics.get().getGraphics().get(0);
190-
selectedGraphic.setSelected(true);
191-
String style = ((SimpleMarkerSymbol) selectedGraphic.getSymbol()).getStyle().toString();
192-
symbolBox.getSelectionModel().select(style);
193-
194-
enableUI(false);
195-
} else {
196-
enableUI(true);
197-
}
198-
} catch (Exception x) {
199-
new Alert(Alert.AlertType.ERROR, "Error identifying clicked graphic").show();
165+
// create a point where the user clicked
166+
mapViewPoint = new Point2D(e.getX(), e.getY());
167+
168+
// identify graphics on the graphics overlay
169+
ListenableFuture<IdentifyGraphicsOverlayResult> identifyGraphics =
170+
mapView.identifyGraphicsOverlayAsync(graphicsOverlay, mapViewPoint, 10, false);
171+
172+
identifyGraphics.addDoneListener(() -> {
173+
try {
174+
if (!identifyGraphics.get().getGraphics().isEmpty()) {
175+
// get the first identified graphic
176+
identifiedGraphic = identifyGraphics.get().getGraphics().get(0);
177+
// select the identified graphic
178+
identifiedGraphic.setSelected(true);
179+
// update the drop down box with the identified graphic's current symbol
180+
String style = ((SimpleMarkerSymbol) identifiedGraphic.getSymbol()).getStyle().toString();
181+
symbolBox.getSelectionModel().select(style);
182+
// show the UI
183+
disableUI(false);
184+
185+
// enable dragging of the identified graphic to move its location
186+
mapView.setOnMouseDragged(event -> {
187+
if (identifiedGraphic.isSelected() && identifiedGraphic != null) {
188+
// set the cursor to closed hand to indicate graphic dragging is active
189+
mapView.setCursor(Cursor.CLOSED_HAND);
190+
// create a point from the dragged location
191+
mapViewPoint = new Point2D(event.getX(), event.getY());
192+
Point mapPoint = mapView.screenToLocation(mapViewPoint);
193+
// update the location of the graphic to the dragged location
194+
identifiedGraphic.setGeometry(mapPoint);
195+
} else new Alert(Alert.AlertType.ERROR, "Error selecting graphic").show();
196+
});
197+
} else {
198+
disableUI(true);
200199
}
201-
});
202-
}
200+
} catch (Exception x) {
201+
new Alert(Alert.AlertType.ERROR, "Error identifying clicked graphic").show();
202+
}
203+
});
203204
}
204205
});
205206

@@ -221,7 +222,7 @@ private void createGraphics() {
221222

222223
Graphic graphic;
223224
// create spatial reference for the points
224-
SpatialReference spatialReference = SpatialReference.create(4326);
225+
SpatialReference spatialReference = SpatialReferences.getWgs84();
225226

226227
// create points to place markers
227228
List<Point> points = new ArrayList<>();
@@ -260,10 +261,14 @@ private void createGraphics() {
260261
}
261262
}
262263

263-
private void enableUI(boolean enable) {
264-
updateDescriptionButton.setDisable(enable);
265-
updateLocationButton.setDisable(enable);
266-
symbolBox.setDisable(enable);
264+
/**
265+
* Disables the visibility of the UI controls.
266+
*
267+
* @param isDisabled visibility of the UI
268+
*/
269+
private void disableUI(boolean isDisabled) {
270+
updateDescriptionButton.setDisable(isDisabled);
271+
symbolBox.setDisable(isDisabled);
267272
}
268273

269274
/**
@@ -286,5 +291,4 @@ public static void main(String[] args) {
286291

287292
Application.launch(args);
288293
}
289-
290294
}

0 commit comments

Comments
 (0)