forked from Esri/arcgis-maps-sdk-dotnet-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManageBookmarks.cs
186 lines (156 loc) · 6.83 KB
/
ManageBookmarks.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
// 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.Linq;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using Foundation;
using UIKit;
namespace ArcGISRuntime.Samples.ManageBookmarks
{
[Register("ManageBookmarks")]
[ArcGISRuntime.Samples.Shared.Attributes.Sample(
"Manage bookmarks",
"Map",
"This sample demonstrates how to access and add bookmarks to a map.",
"")]
public class ManageBookmarks : UIViewController
{
// Hold a reference to the MapView.
private MapView _myMapView;
public ManageBookmarks()
{
Title = "Manage bookmarks";
}
private void Initialize()
{
// Create a map with labeled imagery basemap.
Map map = new Map(Basemap.CreateImageryWithLabels());
// Add default bookmarks.
AddDefaultBookmarks(map);
// Zoom to the last bookmark.
map.InitialViewpoint = map.Bookmarks.Last().Viewpoint;
// Show the map.
_myMapView.Map = map;
}
private void AddDefaultBookmarks(Map map)
{
// Bookmark 1.
// Create a new bookmark.
Bookmark myBookmark = new Bookmark
{
Name = "Mysterious Desert Pattern",
Viewpoint = new Viewpoint(27.3805833, 33.6321389, 6000)
};
// Add the bookmark to bookmark collection of the map.
map.Bookmarks.Add(myBookmark);
// Bookmark 2.
myBookmark = new Bookmark
{
Name = "Dormant Volcano",
Viewpoint = new Viewpoint(-39.299987, 174.060858, 600000)
};
map.Bookmarks.Add(myBookmark);
// Bookmark 3.
myBookmark = new Bookmark
{
Name = "Guitar-Shaped Forest",
Viewpoint = new Viewpoint(-33.867886, -63.985, 40000)
};
map.Bookmarks.Add(myBookmark);
// Bookmark 4.
myBookmark = new Bookmark
{
Name = "Grand Prismatic Spring",
Viewpoint = new Viewpoint(44.525049, -110.83819, 6000)
};
map.Bookmarks.Add(myBookmark);
}
private void OnAddBookmarksButtonClicked(object sender, EventArgs e)
{
// Create Alert for naming the bookmark.
var textInputAlertController = UIAlertController.Create("Provide the bookmark name", "", UIAlertControllerStyle.Alert);
// Add Text Input.
textInputAlertController.AddTextField(textField => { });
// Add Actions.
var cancelAction = UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null);
var okayAction = UIAlertAction.Create("Done", UIAlertActionStyle.Default, alertAction =>
{
// Get the name from the text field.
string name = textInputAlertController.TextFields[0].Text;
// Exit if the name is empty.
if (String.IsNullOrEmpty(name))
return;
// Check to see if there is a bookmark with same name.
bool doesNameExist = _myMapView.Map.Bookmarks.Any(b => b.Name == name);
if (doesNameExist)
return;
// Create a new bookmark.
Bookmark myBookmark = new Bookmark
{
Name = name,
// Get the current viewpoint from map and assign it to bookmark .
Viewpoint = _myMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry)
};
// Add the bookmark to bookmark collection of the map.
_myMapView.Map.Bookmarks.Add(myBookmark);
});
textInputAlertController.AddAction(cancelAction);
textInputAlertController.AddAction(okayAction);
// Show the alert.
PresentViewController(textInputAlertController, true, null);
}
private void OnShowBookmarksButtonClicked(object sender, EventArgs e)
{
// Create a new Alert Controller.
UIAlertController actionAlert = UIAlertController.Create("Bookmarks", "",
UIAlertControllerStyle.Alert);
// Add Bookmarks as Actions.
foreach (Bookmark myBookmark in _myMapView.Map.Bookmarks)
{
actionAlert.AddAction(UIAlertAction.Create(myBookmark.Name, UIAlertActionStyle.Default, action => _myMapView.SetViewpoint(myBookmark.Viewpoint)));
}
// Display the alert.
PresentViewController(actionAlert, true, null);
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
Initialize();
}
public override void LoadView()
{
// Create the views.
View = new UIView {BackgroundColor = UIColor.White};
_myMapView = new MapView();
_myMapView.TranslatesAutoresizingMaskIntoConstraints = false;
UIToolbar toolbar = new UIToolbar();
toolbar.TranslatesAutoresizingMaskIntoConstraints = false;
toolbar.Items = new[]
{
new UIBarButtonItem("Bookmarks", UIBarButtonItemStyle.Plain, OnShowBookmarksButtonClicked),
new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace),
new UIBarButtonItem(UIBarButtonSystemItem.Add, OnAddBookmarksButtonClicked)
};
// Add the views.
View.AddSubviews(_myMapView, toolbar);
// Lay out the views.
NSLayoutConstraint.ActivateConstraints(new[]
{
_myMapView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
_myMapView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
_myMapView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor),
_myMapView.BottomAnchor.ConstraintEqualTo(toolbar.TopAnchor),
toolbar.BottomAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.BottomAnchor),
toolbar.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
toolbar.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor)
});
}
}
}