forked from Esri/arcgis-maps-sdk-dotnet-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeatureCollectionLayerFromPortal.cs
155 lines (125 loc) · 6.18 KB
/
FeatureCollectionLayerFromPortal.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
// 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.Data;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Portal;
using Esri.ArcGISRuntime.UI.Controls;
using Foundation;
using System;
using UIKit;
namespace ArcGISRuntimeXamarin.Samples.FeatureCollectionLayerFromPortal
{
[Register("FeatureCollectionLayerFromPortal")]
public class FeatureCollectionLayerFromPortal : UIViewController
{
// Reference to the MapView used in the app
private MapView _myMapView;
// Default portal item Id to load features from
private const string FeatureCollectionItemId = "5ffe7733754f44a9af12a489250fe12b";
// Text field for specifying a portal item Id
UITextField _collectionItemIdTextBox;
UIButton _addFeaturesButton;
public FeatureCollectionLayerFromPortal()
{
Title = "Create a feature collection layer from a portal item";
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Create the layout
CreateLayout();
// Initialize the app
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 - 50);
_collectionItemIdTextBox.Frame = new CoreGraphics.CGRect(10, View.Bounds.Height - 50, View.Bounds.Width - 10, 20);
_addFeaturesButton.Frame = new CoreGraphics.CGRect(0, View.Bounds.Height -30, View.Bounds.Width, 30);
base.ViewDidLayoutSubviews();
}
private void Initialize()
{
// Add a default value for the portal item Id
_collectionItemIdTextBox.Text = FeatureCollectionItemId;
// Create a new map with the oceans basemap and add it to the map view
Map myMap = new Map(Basemap.CreateOceans());
_myMapView.Map = myMap;
}
private async void OpenFeaturesFromArcGISOnline(string itemId)
{
try
{
// Open a portal item containing a feature collection
ArcGISPortal portal = await ArcGISPortal.CreateAsync();
PortalItem collectionItem = await PortalItem.CreateAsync(portal, itemId);
// Verify that the item is a feature collection
if (collectionItem.Type == PortalItemType.FeatureCollection)
{
// Create a new FeatureCollection from the item
FeatureCollection featureCollection = new FeatureCollection(collectionItem);
// Create a layer to display the collection and add it to the map as an operational layer
FeatureCollectionLayer featureCollectionLayer = new FeatureCollectionLayer(featureCollection);
featureCollectionLayer.Name = collectionItem.Title;
_myMapView.Map.OperationalLayers.Add(featureCollectionLayer);
}
else
{
var alert = new UIAlertView("Feature Collection", "Portal item with ID '" + itemId + "' is not a feature collection.", null, "OK");
alert.Show();
}
}
catch (Exception ex)
{
var alert = new UIAlertView("Error", "Unable to open item with ID '" + itemId + "': " + ex.Message, null, "OK");
alert.Show();
}
}
private void OpenPortalFeatureCollectionClick(object sender, EventArgs e)
{
// Get the portal item Id from the user
var collectionItemId = _collectionItemIdTextBox.Text.Trim();
// Make sure an Id was entered
if (string.IsNullOrEmpty(collectionItemId))
{
var alert = new UIAlertView("Feature Collection ID", "Please enter a portal item ID", null, "OK");
alert.Show();
return;
}
// Call a function to add the feature collection from the specified portal item
OpenFeaturesFromArcGISOnline(collectionItemId);
}
private void CreateLayout()
{
// Store the main view's width and height
var appViewWidth = View.Bounds.Width;
var appViewHeight = View.Bounds.Height;
// Define an offset from the top of the page (to account for the iOS status bar)
var yPageOffset = 60;
// Create a new MapView
_myMapView = new MapView();
// Create a text input for the portal item Id
_collectionItemIdTextBox = new UITextField();
_collectionItemIdTextBox.BackgroundColor = UIColor.LightGray;
// Allow pressing 'return' to dismiss the keyboard
_collectionItemIdTextBox.ShouldReturn += (textField) => { textField.ResignFirstResponder(); return true; };
// Create a button for adding features from a portal item
_addFeaturesButton = new UIButton(UIButtonType.Custom);
_addFeaturesButton.SetTitle("Add from portal item", UIControlState.Normal);
_addFeaturesButton.SetTitleColor(UIColor.Blue, UIControlState.Normal);
_addFeaturesButton.BackgroundColor = UIColor.White;
// Assign a click handler to the UIButton
_addFeaturesButton.TouchUpInside += OpenPortalFeatureCollectionClick;
// Add the MapView, UITextField, and UIButton to the page
View.AddSubviews(_myMapView, _collectionItemIdTextBox, _addFeaturesButton);
View.BackgroundColor = UIColor.White;
}
}
}