forked from Esri/arcgis-maps-sdk-dotnet-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplayLayerViewState.cs
253 lines (209 loc) · 9.54 KB
/
DisplayLayerViewState.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
// Copyright 2016 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 System;
using System.Collections.Generic;
using System.Linq;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using Foundation;
using UIKit;
namespace ArcGISRuntime.Samples.DisplayLayerViewState
{
[Register("DisplayLayerViewState")]
[ArcGISRuntime.Samples.Shared.Attributes.Sample(
"Display layer view state",
"MapView",
"This sample demonstrates how to get view status for layers in a map.",
"")]
public class DisplayLayerViewState : UIViewController
{
// Hold references to the UI controls.
private MapView _myMapView;
private UITableView _tableView;
// Reference to list of view status for each layer.
private readonly List<LayerStatusModel> _layerStatusModels = new List<LayerStatusModel>();
public DisplayLayerViewState()
{
Title = "Display Layer View State";
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
Initialize();
}
private void Initialize()
{
// Create a new Map.
Map myMap = new Map();
// Create the URL for the tiled layer.
Uri tiledLayerUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer");
// Create a tiled layer from the URL
ArcGISTiledLayer tiledLayer = new ArcGISTiledLayer(tiledLayerUri)
{
Name = "Tiled Layer"
};
// Add the tiled layer to map.
myMap.OperationalLayers.Add(tiledLayer);
// Create the URL for the ArcGISMapImage layer.
Uri imageLayerUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer");
// Create ArcGISMapImage layer using a URL.
ArcGISMapImageLayer imageLayer = new ArcGISMapImageLayer(imageLayerUri)
{
Name = "Image Layer",
// Set the visible scale range for the image layer.
MinScale = 40000000,
MaxScale = 2000000
};
// Add the image layer to map.
myMap.OperationalLayers.Add(imageLayer);
// Create Uri for feature layer.
Uri featureLayerUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Recreation/FeatureServer/0");
// Create a feature layer using URL.
FeatureLayer myFeatureLayer = new FeatureLayer(featureLayerUri)
{
Name = "Feature Layer"
};
// Add the feature layer to map.
myMap.OperationalLayers.Add(myFeatureLayer);
// Create a point the map should zoom to.
MapPoint mapPoint = new MapPoint(-11000000, 4500000, SpatialReferences.WebMercator);
// Set the initial viewpoint for map.
myMap.InitialViewpoint = new Viewpoint(mapPoint, 50000000);
// Initialize the model list with unknown status for each layer.
foreach (Layer layer in myMap.OperationalLayers)
{
_layerStatusModels.Add(new LayerStatusModel(layer.Name, "Unknown"));
}
// Create the table view source and pass the list of models to it.
_tableView.Source = new LayerViewStatusTableSource(_layerStatusModels);
// Event for layer view state changed.
_myMapView.LayerViewStateChanged += OnLayerViewStateChanged;
// Provide used Map to the MapView.
_myMapView.Map = myMap;
}
private void OnLayerViewStateChanged(object sender, LayerViewStateChangedEventArgs e)
{
// State changed event is sent by a layer. In the list, find the layer which sends this event.
// If it exists then update the status.
LayerStatusModel model = _layerStatusModels.FirstOrDefault(l => l.LayerName == e.Layer.Name);
if (model != null)
{
model.LayerViewStatus = e.LayerViewState.Status.ToString();
}
// Update the table.
_tableView.ReloadData();
}
public override void LoadView()
{
_myMapView = new MapView();
_tableView = new UITableView();
_tableView.RowHeight = 40;
_myMapView.TranslatesAutoresizingMaskIntoConstraints = false;
_tableView.TranslatesAutoresizingMaskIntoConstraints = false;
View = new UIView();
View.AddSubviews(_myMapView, _tableView);
}
public override void TraitCollectionDidChange(UITraitCollection previousTraitCollection)
{
base.TraitCollectionDidChange(previousTraitCollection);
// Reset constraints.
_tableView.RemoveFromSuperview();
_myMapView.RemoveFromSuperview();
View.AddSubviews(_myMapView, _tableView);
if (View.TraitCollection.VerticalSizeClass == UIUserInterfaceSizeClass.Compact)
{
applyLandscapeLayout();
}
else
{
applyPortraitLayout();
}
}
private void applyPortraitLayout()
{
_tableView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor).Active = true;
_tableView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor).Active = true;
_tableView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor).Active = true;
_tableView.HeightAnchor.ConstraintEqualTo(120).Active = true;
_myMapView.TopAnchor.ConstraintEqualTo(_tableView.BottomAnchor).Active = true;
_myMapView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor).Active = true;
_myMapView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor).Active = true;
_myMapView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor).Active = true;
}
private void applyLandscapeLayout()
{
_tableView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor).Active = true;
_tableView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor).Active = true;
_tableView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor).Active = true;
_tableView.WidthAnchor.ConstraintEqualTo(View.Frame.Height).Active = true;
_myMapView.TopAnchor.ConstraintEqualTo(View.TopAnchor).Active = true;
_myMapView.LeadingAnchor.ConstraintEqualTo(_tableView.TrailingAnchor).Active = true;
_myMapView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor).Active = true;
_myMapView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor).Active = true;
}
}
/// <summary>
/// Class that is used by the table view to populate itself
/// </summary>
internal class LayerViewStatusTableSource : UITableViewSource
{
// List of layer status model.
private readonly List<LayerStatusModel> _layers;
// Identifier for the table cell.
private const string CellIdentifier = "TableCell";
public LayerViewStatusTableSource(List<LayerStatusModel> layers)
{
_layers = layers;
}
/// <summary>
/// Called by the TableView to determine how many sections(groups) there are.
/// </summary>
public override nint NumberOfSections(UITableView tableView)
{
return 1;
}
/// <summary>
/// Called by the TableView to determine how many cells to create for that particular section.
/// </summary>
public override nint RowsInSection(UITableView tableview, nint section)
{
return _layers?.Count ?? 0;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
// Get the reused cell from the table view.
UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier);
// Get the model at this current cell index.
LayerStatusModel model = _layers[indexPath.Row];
// If there are no cells to reuse-create one.
// We are specifically using Value1 style so text for both layer name and status can be displayed.
if (cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Value1, CellIdentifier);
}
cell.TextLabel.Text = model.LayerName ?? "Layer " + indexPath.Row;
cell.DetailTextLabel.Text = model.LayerViewStatus;
return cell;
}
}
/// <summary>
/// This is a custom class that holds information for layer name and status.
/// </summary>
internal class LayerStatusModel
{
internal string LayerName { get; }
internal string LayerViewStatus { get; set; }
public LayerStatusModel(string layerName, string layerStatus)
{
LayerName = layerName;
LayerViewStatus = layerStatus;
}
}
}