forked from Esri/arcgis-maps-sdk-dotnet-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapViewModel.cs
135 lines (119 loc) · 4.38 KB
/
MapViewModel.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
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Toolkit.UI;
using Esri.ArcGISRuntime.UI;
using Esri.ArcGISRuntime.UI.Controls;
using Esri.ArcGISRuntime.UI.Editing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace EditorDemo
{
public partial class MapViewModel : ObservableObject
{
public MapViewModel()
{
LoadMap();
}
private async void LoadMap()
{
Map = await MapCreator.CreateOgcConformanceMap(true);
}
public MapViewController Controller { get; } = new MapViewController();
[ObservableProperty]
private Map? _map;
partial void OnMapChanging(Map? value)
{
// Clean up
Controller.DismissCallout();
Selection = null;
EditFeatureSelection = null;
}
public GraphicsOverlayCollection? GraphicsOverlays { get; } = new GraphicsOverlayCollection();
[RelayCommand]
public async Task OnGeoViewTapped(GeoViewInputEventArgs eventArgs) => await Identify(eventArgs.Position, eventArgs.Location);
public async Task Identify(Point location, MapPoint? mapLocation)
{
if (EditFeatureSelection is not null)
return; // Don't do identify during an edit session
Controller.DismissCallout();
Selection = null;
var result = await Controller.IdentifyLayersAsync(location, 1);
if (result.FirstOrDefault()?.GeoElements?.FirstOrDefault() is GeoElement element)
{
var def = new CalloutDefinition(element) { TextExpression = "$feature.name" };
Selection = element;
def.Tag = element;
if (EditorToolbar.CanEditGeometry(element))
{
def.ButtonImage = new RuntimeImage(new Uri("edit-geometry-16.png", UriKind.Relative));
def.OnButtonClick = (obj) => Edit(obj as GeoElement);
}
Controller.ShowCalloutForGeoElement(element, location, def);
}
}
private void Edit(GeoElement? element)
{
if (element?.Geometry is not null)
{
EditFeatureSelection = element;
Controller.DismissCallout();
Controller.SetViewpointGeometryAsync(element.Geometry, 100);
}
}
[RelayCommand]
public void OnEditingCompleted(EditorToolbar.EditingCompletedEventArgs e)
{
if (EditFeatureSelection != null)
{
EditFeatureSelection.Geometry = e.Geometry;
if(EditFeatureSelection is Feature feature && feature.FeatureTable?.CanUpdate(feature) == true)
{
feature.FeatureTable.UpdateFeatureAsync(feature);
}
EditFeatureSelection = null;
}
}
[RelayCommand]
public void OnEditingCancelled()
{
EditFeatureSelection = null;
}
[ObservableProperty]
private GeoElement? _editFeatureSelection;
[ObservableProperty]
private GeoElement? _selection;
partial void OnSelectionChanged(GeoElement? oldValue, GeoElement? newValue)
{
Select(oldValue, false);
Select(newValue, true);
}
private void Select(GeoElement? value, bool selected)
{
if (value is Graphic g)
g.IsSelected = selected;
else if (value is Feature feature && feature.FeatureTable?.Layer is FeatureLayer layer)
{
if (selected) layer.SelectFeature(feature);
else layer.UnselectFeature(feature);
}
}
}
public class MapViewController : GeoViewController
{
public Task<bool> SetViewpointGeometryAsync(Geometry? boundingGeometry, double padding = 0d)
{
if(base.ConnectedView is MapView mapView && boundingGeometry is not null)
{
return mapView.SetViewpointGeometryAsync(boundingGeometry, padding);
}
return Task.FromResult(false);
}
}
}