forked from Esri/arcgis-maps-sdk-dotnet-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSketchOnMap.cs
452 lines (392 loc) · 17.5 KB
/
SketchOnMap.cs
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright 2018 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.
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using Esri.ArcGISRuntime.UI.Controls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
namespace ArcGISRuntime.Samples.SketchOnMap
{
[Activity]
[ArcGISRuntime.Samples.Shared.Attributes.Sample(
"Sketch graphics on the map",
"GraphicsOverlay",
"This sample demonstrates how to interactively sketch and edit graphics in the map view and display them in a graphics overlay. You can sketch a variety of geometry types and undo or redo operations.",
"1. Click the 'Sketch' button.\n2. Choose a sketch type from the drop down list.\n3. While sketching, you can undo/redo operations.\n4. Click 'Done' to finish the sketch.\n5. Click 'Edit', then click a graphic to start editing.\n6. Make edits then click 'Done' or 'Cancel' to finish editing.")]
public class SketchOnMap : Activity
{
// Create and hold reference to the used MapView
private MapView _myMapView = new MapView();
// Dictionary to hold sketch mode enum names and values
private Dictionary<string, int> _sketchModeDictionary;
// Graphics overlay to host sketch graphics
private GraphicsOverlay _sketchOverlay;
// Buttons for interacting with the SketchEditor
private Button _editButton;
private Button _undoButton;
private Button _redoButton;
private Button _doneButton;
private Button _clearButton;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Title = "Sketch on map";
// Create the UI
CreateLayout();
// Initialize controls, set up event handlers, etc.
Initialize();
}
private void Initialize()
{
// Create a light gray canvas map
Map myMap = new Map(Basemap.CreateLightGrayCanvas());
// Create graphics overlay to display sketch geometry
_sketchOverlay = new GraphicsOverlay();
_myMapView.GraphicsOverlays.Add(_sketchOverlay);
// Assign the map to the MapView
_myMapView.Map = myMap;
// Set the sketch editor configuration to allow vertex editing, resizing, and moving
SketchEditConfiguration config = _myMapView.SketchEditor.EditConfiguration;
config.AllowVertexEditing = true;
config.ResizeMode = SketchResizeMode.Uniform;
config.AllowMove = true;
// Listen to the sketch editor tools CanExecuteChange so controls can be enabled/disabled
_myMapView.SketchEditor.UndoCommand.CanExecuteChanged += CanExecuteChanged;
_myMapView.SketchEditor.RedoCommand.CanExecuteChanged += CanExecuteChanged;
_myMapView.SketchEditor.CompleteCommand.CanExecuteChanged += CanExecuteChanged;
// Listen to collection changed event on the graphics overlay to enable/disable controls that require a graphic
_sketchOverlay.Graphics.CollectionChanged += GraphicsChanged;
}
private void CreateLayout()
{
// Create horizontal layouts for the buttons at the top
LinearLayout buttonLayoutOne = new LinearLayout(this) { Orientation = Orientation.Horizontal };
LinearLayout buttonLayoutTwo = new LinearLayout(this) { Orientation = Orientation.Horizontal };
// Parameters for all of the buttons. Used to set buttons height and width.
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.MatchParent,
1.0f
);
// Button to sketch a selected geometry type on the map view
Button sketchButton = new Button(this)
{
Text = "Sketch",
LayoutParameters = param
};
sketchButton.Click += OnSketchClicked;
// Button to edit an existing graphic's geometry
_editButton = new Button(this)
{
Text = "Edit",
LayoutParameters = param
};
_editButton.Click += OnEditClicked;
_editButton.Enabled = false;
// Buttons to Undo/Redo sketch and edit operations
_undoButton = new Button(this)
{
Text = "Undo",
LayoutParameters = param
};
_undoButton.Click += OnUndoClicked;
_undoButton.Enabled = false;
_redoButton = new Button(this)
{
Text = "Redo",
LayoutParameters = param
};
_redoButton.Click += OnRedoClicked;
_redoButton.Enabled = false;
// Button to complete the sketch or edit
_doneButton = new Button(this)
{
Text = "Done",
LayoutParameters = param
};
_doneButton.Click += OnCompleteClicked;
_doneButton.Enabled = false;
// Button to clear all graphics and sketches
_clearButton = new Button(this)
{
Text = "Clear",
LayoutParameters = param
};
_clearButton.Click += OnClearClicked;
_clearButton.Enabled = false;
// Add all sketch controls (buttons) to the button bars
buttonLayoutOne.AddView(sketchButton);
buttonLayoutOne.AddView(_editButton);
buttonLayoutOne.AddView(_clearButton);
// Second button bar
buttonLayoutTwo.AddView(_undoButton);
buttonLayoutTwo.AddView(_redoButton);
buttonLayoutTwo.AddView(_doneButton);
// Create a new vertical layout for the app (buttons followed by map view)
LinearLayout mainLayout = new LinearLayout(this) { Orientation = Orientation.Vertical };
// Add the button layouts
mainLayout.AddView(buttonLayoutOne);
mainLayout.AddView(buttonLayoutTwo);
// Add the map view to the layout
mainLayout.AddView(_myMapView);
// Show the layout in the app
SetContentView(mainLayout);
}
private void OnSketchClicked(object sender, EventArgs e)
{
Button sketchButton = (Button)sender;
// Create a dictionary of enum names and values
IEnumerable<int> enumValues = Enum.GetValues(typeof(SketchCreationMode)).Cast<int>();
_sketchModeDictionary = enumValues.ToDictionary(v => Enum.GetName(typeof(SketchCreationMode), v), v => v);
// Create a menu to show sketch modes
PopupMenu sketchModesMenu = new PopupMenu(sketchButton.Context, sketchButton);
sketchModesMenu.MenuItemClick += OnSketchModeItemClicked;
// Create a menu option for each basemap type
foreach (string mode in _sketchModeDictionary.Keys)
{
sketchModesMenu.Menu.Add(mode);
}
// Show menu in the view
sketchModesMenu.Show();
}
private async void OnSketchModeItemClicked(object sender, PopupMenu.MenuItemClickEventArgs e)
{
try
{
// Get the title of the selected menu item (sketch mode)
string sketchModeName = e.Item.TitleCondensedFormatted.ToString();
// Let the user draw on the map view using the chosen sketch mode
SketchCreationMode creationMode = (SketchCreationMode)_sketchModeDictionary[sketchModeName];
Geometry geometry = await _myMapView.SketchEditor.StartAsync(creationMode, true);
// Create and add a graphic from the geometry the user drew
Graphic graphic = CreateGraphic(geometry);
_sketchOverlay.Graphics.Add(graphic);
}
catch (TaskCanceledException)
{
// Ignore ... let the user cancel drawing
}
catch (Exception ex)
{
// Report exceptions
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.SetTitle("Error drawing graphic shape");
alertBuilder.SetMessage(ex.Message);
alertBuilder.Show();
}
}
private async void OnEditClicked(object sender, EventArgs e)
{
try
{
// Allow the user to select a graphic
Graphic editGraphic = await GetGraphicAsync();
if (editGraphic == null) { return; }
// Let the user make changes to the graphic's geometry, await the result (updated geometry)
Geometry newGeometry = await _myMapView.SketchEditor.StartAsync(editGraphic.Geometry);
// Display the updated geometry in the graphic
editGraphic.Geometry = newGeometry;
}
catch (TaskCanceledException)
{
// Ignore ... let the user cancel editing
}
catch (Exception ex)
{
// Report exceptions
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.SetTitle("Error editing shape");
alertBuilder.SetMessage(ex.Message);
alertBuilder.Show();
}
}
private void OnClearClicked(object sender, EventArgs e)
{
// Remove all graphics from the graphics overlay
_sketchOverlay.Graphics.Clear();
// Cancel any uncompleted sketch
if (_myMapView.SketchEditor.CancelCommand.CanExecute(null))
{
_myMapView.SketchEditor.CancelCommand.Execute(null);
}
}
private void OnCompleteClicked(object sender, EventArgs e)
{
// Execute the CompleteCommand on the sketch editor
if (_myMapView.SketchEditor.CompleteCommand.CanExecute(null))
{
_myMapView.SketchEditor.CompleteCommand.Execute(null);
}
}
private void OnRedoClicked(object sender, EventArgs e)
{
// Execute the RedoCommand on the sketch editor
if (_myMapView.SketchEditor.RedoCommand.CanExecute(null))
{
_myMapView.SketchEditor.RedoCommand.Execute(null);
}
}
private void OnUndoClicked(object sender, EventArgs e)
{
// Execute the UndoCommand on the sketch editor
if (_myMapView.SketchEditor.UndoCommand.CanExecute(null))
{
_myMapView.SketchEditor.UndoCommand.Execute(null);
}
}
private void GraphicsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
// Enable or disable the clear and edit buttons depending on whether or not graphics exist
bool haveGraphics = _sketchOverlay.Graphics.Count > 0;
_editButton.Enabled = haveGraphics;
_clearButton.Enabled = haveGraphics;
}
private void CanExecuteChanged(object sender, EventArgs e)
{
// Enable or disable the corresponding command for the sketch editor
ICommand command = (ICommand)sender;
if (command == _myMapView.SketchEditor.UndoCommand)
{
_undoButton.Enabled = command.CanExecute(null);
}
else if (command == _myMapView.SketchEditor.RedoCommand)
{
_redoButton.Enabled = command.CanExecute(null);
}
else if (command == _myMapView.SketchEditor.CompleteCommand)
{
_doneButton.Enabled = command.CanExecute(null);
}
}
#region Graphic and symbol helpers
private Graphic CreateGraphic(Geometry geometry)
{
// Create a graphic to display the specified geometry
Symbol symbol = null;
switch (geometry.GeometryType)
{
// Symbolize with a fill symbol
case GeometryType.Envelope:
case GeometryType.Polygon:
{
symbol = new SimpleFillSymbol()
{
Color = Color.Red,
Style = SimpleFillSymbolStyle.Solid
};
break;
}
// Symbolize with a line symbol
case GeometryType.Polyline:
{
symbol = new SimpleLineSymbol()
{
Color = Color.Red,
Style = SimpleLineSymbolStyle.Solid,
Width = 5d
};
break;
}
// Symbolize with a marker symbol
case GeometryType.Point:
case GeometryType.Multipoint:
{
symbol = new SimpleMarkerSymbol()
{
Color = Color.Red,
Style = SimpleMarkerSymbolStyle.Circle,
Size = 15d
};
break;
}
}
// pass back a new graphic with the appropriate symbol
return new Graphic(geometry, symbol);
}
private async Task<Graphic> GetGraphicAsync()
{
// Wait for the user to click a location on the map
Geometry mapPoint = await _myMapView.SketchEditor.StartAsync(SketchCreationMode.Point, false);
// Convert the map point to a screen point
Android.Graphics.PointF screenCoordinate = _myMapView.LocationToScreen((MapPoint)mapPoint);
// Identify graphics in the graphics overlay using the point
IReadOnlyList<IdentifyGraphicsOverlayResult> results = await _myMapView.IdentifyGraphicsOverlaysAsync(screenCoordinate, 2, false);
// If results were found, get the first graphic
Graphic graphic = null;
IdentifyGraphicsOverlayResult idResult = results.FirstOrDefault();
if (idResult != null && idResult.Graphics.Count > 0)
{
graphic = idResult.Graphics.FirstOrDefault();
}
// Return the graphic (or null if none were found)
return graphic;
}
#endregion Graphic and symbol helpers
private async void SketchGeometry(string sketchModeName)
{
try
{
// Let the user draw on the map view using the chosen sketch mode
SketchCreationMode creationMode = (SketchCreationMode)_sketchModeDictionary[sketchModeName];
Geometry geometry = await _myMapView.SketchEditor.StartAsync(creationMode, true);
// Create and add a graphic from the geometry the user drew
Graphic graphic = CreateGraphic(geometry);
_sketchOverlay.Graphics.Add(graphic);
}
catch (TaskCanceledException)
{
// Ignore ... let the user cancel drawing
}
catch (Exception ex)
{
// Report exceptions
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.SetTitle("Error drawing graphic shape");
alertBuilder.SetMessage(ex.Message);
alertBuilder.Show();
}
}
private async void EditGraphic()
{
try
{
// Allow the user to select a graphic
Graphic editGraphic = await GetGraphicAsync();
if (editGraphic == null) { return; }
// Let the user make changes to the graphic's geometry, await the result (updated geometry)
Geometry newGeometry = await _myMapView.SketchEditor.StartAsync(editGraphic.Geometry);
// Display the updated geometry in the graphic
editGraphic.Geometry = newGeometry;
}
catch (TaskCanceledException)
{
// Ignore ... let the user cancel editing
}
catch (Exception ex)
{
// Report exceptions
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.SetTitle("Error editing shape");
alertBuilder.SetMessage(ex.Message);
alertBuilder.Show();
}
}
}
}