forked from Esri/arcgis-maps-sdk-dotnet-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSketchOnMap.xaml.cs
216 lines (187 loc) · 8.67 KB
/
SketchOnMap.xaml.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
// Copyright 2017 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 Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using System;
using System.Linq;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.Xaml;
namespace ArcGISRuntime.UWP.Samples.SketchOnMap
{
[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. Click the 'Draw' button and click in the map view to create the sketch.\n4. While sketching, you can undo/redo operations.\n5. Click 'Done' to finish the sketch.\n6. To edit a graphic, click 'Draw/Edit', then 'Edit'. Select a graphic in the map to start editing.\n7. Make edits then click 'Done' to finish editing.")]
public sealed partial class SketchOnMap
{
// Graphics overlay to host sketch graphics
private GraphicsOverlay _sketchOverlay;
public SketchOnMap()
{
InitializeComponent();
// Call a function to set up the map and sketch editor
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;
// Fill the combo box with choices for the sketch modes (shapes)
SketchModeComboBox.ItemsSource = System.Enum.GetValues(typeof(SketchCreationMode));
SketchModeComboBox.SelectedIndex = 0;
// Set the sketch editor configuration to allow vertex editing, resizing, and moving
var config = MyMapView.SketchEditor.EditConfiguration;
config.AllowVertexEditing = true;
config.ResizeMode = SketchResizeMode.Uniform;
config.AllowMove = true;
// Set the sketch editor as the page's data context
DataContext = MyMapView.SketchEditor;
}
#region Graphic and symbol helpers
private Graphic CreateGraphic(Esri.ArcGISRuntime.Geometry.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 = Colors.Red,
Style = SimpleFillSymbolStyle.Solid,
};
break;
}
// Symbolize with a line symbol
case GeometryType.Polyline:
{
symbol = new SimpleLineSymbol()
{
Color = Colors.Red,
Style = SimpleLineSymbolStyle.Solid,
Width = 5d
};
break;
}
// Symbolize with a marker symbol
case GeometryType.Point:
case GeometryType.Multipoint:
{
symbol = new SimpleMarkerSymbol()
{
Color = Colors.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
var mapPoint = (MapPoint)await MyMapView.SketchEditor.StartAsync(SketchCreationMode.Point, false);
// Convert the map point to a screen point
var screenCoordinate = MyMapView.LocationToScreen(mapPoint);
// Identify graphics in the graphics overlay using the point
var 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
private async void DrawButtonClick(object sender, RoutedEventArgs e)
{
try
{
// Set the focus to the map view(close the flyout panel)
DrawToolsFlyout.Hide();
// Let the user draw on the map view using the chosen sketch mode
SketchCreationMode creationMode = (SketchCreationMode)SketchModeComboBox.SelectedItem;
Esri.ArcGISRuntime.Geometry.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);
// Enable/disable the clear and edit buttons according to whether or not graphics exist in the overlay
ClearButton.IsEnabled = _sketchOverlay.Graphics.Count > 0;
EditButton.IsEnabled = _sketchOverlay.Graphics.Count > 0;
}
catch (TaskCanceledException)
{
// Ignore ... let the user cancel drawing
}
catch (Exception ex)
{
// Report exceptions
var dialog = new MessageDialog("Error drawing graphic shape: " + ex.Message);
await dialog.ShowAsync();
}
}
private void ClearButtonClick(object sender, RoutedEventArgs 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);
}
// Disable buttons that require graphics
ClearButton.IsEnabled = false;
EditButton.IsEnabled = false;
}
private async void EditButtonClick(object sender, RoutedEventArgs e)
{
try
{
// Set the focus to the map view(close the flyout panel)
DrawToolsFlyout.Hide();
// 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
var dialog = new MessageDialog("Error editing shape: " + ex.Message);
await dialog.ShowAsync();
}
}
}
}