forked from Esri/arcgis-maps-sdk-dotnet-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplayLayerViewState.cs
223 lines (178 loc) · 8.2 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
// 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 Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
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
{
// Create and hold reference to the used MapView
private MapView _myMapView = new MapView();
// Create and hold reference to tableview
private UITableView _tableView;
// Reference to list of view status for each layer
private List<LayerStatusModel> _layerStatusModels = new List<LayerStatusModel>();
public DisplayLayerViewState()
{
Title = "Display Layer View State";
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
CreateLayout();
Initialize();
}
public override void ViewDidLayoutSubviews()
{
// Setup the visual frame for the MapView
_myMapView.Frame = new CoreGraphics.CGRect(0, 0, View.Bounds.Width, View.Bounds.Height -120);
_tableView.Frame = new CoreGraphics.CGRect(0, _myMapView.Frame.Height, View.Bounds.Width, 120);
base.ViewDidLayoutSubviews();
}
private void Initialize()
{
// Create new Map
Map myMap = new Map();
// Create the uri for the tiled layer
Uri tiledLayerUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer");
// Create a tiled layer using url
ArcGISTiledLayer tiledLayer = new ArcGISTiledLayer(tiledLayerUri);
tiledLayer.Name = "Tiled Layer";
// Add the tiled layer to map
myMap.OperationalLayers.Add(tiledLayer);
// Create the uri for the ArcGISMapImage layer
var imageLayerUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer");
// Create ArcGISMapImage layer using a url
ArcGISMapImageLayer imageLayer = new ArcGISMapImageLayer(imageLayerUri);
imageLayer.Name = "Image Layer";
// Set the visible scale range for the image layer
imageLayer.MinScale = 40000000;
imageLayer.MaxScale = 2000000;
// Add the image layer to map
myMap.OperationalLayers.Add(imageLayer);
//Create Uri for feature layer
var featureLayerUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Recreation/FeatureServer/0");
//Create a feature layer using url
FeatureLayer myFeatureLayer = new FeatureLayer(featureLayerUri);
myFeatureLayer.Name = "Feature Layer";
// Add the feature layer to map
myMap.OperationalLayers.Add(myFeatureLayer);
// Create a mappoint 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 tableview 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
var model = _layerStatusModels.FirstOrDefault(l => l.LayerName == e.Layer.Name);
if (model != null)
model.LayerViewStatus = e.LayerViewState.Status.ToString();
// Update the table
_tableView.ReloadData();
}
private void CreateLayout()
{
//set up UIStackView for laying out controls
var stackView = new UIStackView(new CoreGraphics.CGRect(0, 0, View.Bounds.Width, View.Bounds.Height));
stackView.Axis = UILayoutConstraintAxis.Vertical;
stackView.Alignment = UIStackViewAlignment.Fill;
stackView.Distribution = UIStackViewDistribution.FillProportionally;
stackView.BackgroundColor = UIColor.Gray;
_myMapView = new MapView();
stackView.AddArrangedSubview(_myMapView);
// Create a tableview for displaying layer view status for each layer
_tableView = new UITableView();
stackView.AddArrangedSubview(_tableView);
// Add MapView to the page
View.AddSubviews(stackView);
}
}
/// <summary>
/// Class that is used by the table view to populate itself
/// </summary>
internal class LayerViewStatusTableSource: UITableViewSource
{
// List of layer status model
protected List<LayerStatusModel> _layers;
// Identifier for the table cell
protected 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 != null?_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; private set; }
internal string LayerViewStatus { get; set; }
public LayerStatusModel(string layerName, string layerStatus)
{
LayerName = layerName;
LayerViewStatus = layerStatus;
}
}
}