forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSM.cs
243 lines (224 loc) · 9.04 KB
/
OSM.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
#region Copyright Syncfusion Inc. 2001-2018.
// Copyright Syncfusion Inc. 2001-2018. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// licensing@syncfusion.com. Any infringement will be prosecuted under
// applicable laws.
#endregion
using Syncfusion.SfBusyIndicator.iOS;
using System;
using System.Net;
using System.Threading.Tasks;
using Syncfusion.SfMaps.iOS;
using Foundation;
using CoreGraphics;
using UIKit;
namespace SampleBrowser
{
public class OSM : SampleView
{
bool TapGesture_ShouldReceiveTouch(UIGestureRecognizer recognizer, UITouch touch)
{
if (touch.TapCount == 1)
{
UIApplication.SharedApplication.OpenUrl(new NSUrl("https://www.openstreetmap.org/copyright"));
}
return true;
}
internal SfBusyIndicator busyindicator;
UIView view;
UIView mainGrid;
UIView childGrid;
double previousWidth;
double previousHeight;
CGSize label1Size;
CGSize label2Size;
UILabel label1;
UILabel label2;
bool isDisposed;
SFMap maps;
UILabel label;
bool isConnected;
public override void LayoutSubviews()
{
view.Frame = new CGRect(Frame.Location.X, 0, Frame.Size.Width, Frame.Size.Height);
busyindicator.Frame = new CGRect(Frame.Location.X, 0, Frame.Size.Width, Frame.Size.Height);
if (childGrid != null)
childGrid.Frame = new CGRect(Frame.Location.X, 0, Frame.Size.Width, Frame.Size.Height);
if (mainGrid != null)
mainGrid.Frame = new CGRect(Frame.Location.X, 0, Frame.Size.Width, Frame.Size.Height);
if (maps != null)
maps.Frame = new CGRect(Frame.Location.X, 0, Frame.Size.Width, Frame.Size.Height);
if (label1 != null && label2 != null)
{
label1.Frame = new CGRect(Frame.Size.Width - (label1Size.Width + label2Size.Width) - 4,
Frame.Size.Height - label2Size.Height - 4,
label1Size.Width + 4, label1Size.Height + 4);
label2.Frame = new CGRect(Frame.Size.Width - label2Size.Width - 2,
Frame.Size.Height - label2Size.Height - 4,
label2Size.Width + 4, label2Size.Height + 4);
}
if (childGrid != null && childGrid.Subviews.Length > 0
&& previousWidth == view.Frame.Size.Width && previousHeight == view.Frame.Size.Height)
return;
double row = 0;
double column = 0;
if (view.Frame.Size.Height > 0)
{
row = view.Frame.Size.Height / 512;
if (Math.Ceiling(row) <= 0)
row = 1;
else
row = Math.Ceiling(row);
previousHeight = view.Frame.Size.Height;
}
if (view.Frame.Size.Width > 0)
{
column = view.Frame.Size.Width / 512;
if (Math.Ceiling(column) <= 0)
column = 1;
else
column = Math.Ceiling(column);
previousWidth = view.Frame.Size.Width;
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
UIImageView image = new UIImageView();
image.Image = UIImage.FromBundle("grid.png");
var xPos = i * 512;
var yPos = j * 512;
image.Frame = new CGRect(xPos, yPos, 512, 512);
childGrid.AddSubview(image);
SetNeedsDisplay();
}
}
childGrid.ClipsToBounds = true;
SetNeedsDisplay();
}
public OSM()
{
view = new UIView();
view.Frame = new CGRect(0, 0, 300, 400);
AddMainView();
busyindicator = new SfBusyIndicator();
busyindicator.ViewBoxWidth = 75;
busyindicator.ViewBoxHeight = 75;
busyindicator.Foreground = UIColor.FromRGB(0x77, 0x97, 0x72); /*#779772*/
busyindicator.AnimationType = SFBusyIndicatorAnimationType.SFBusyIndicatorAnimationTypeSlicedCircle;
IsConnectedToNetwork();
}
protected override void Dispose(bool disposing)
{
isDisposed = true;
base.Dispose(disposing);
}
async void IsConnectedToNetwork()
{
isConnected = await IsConnected();
if (isDisposed)
return;
if (isConnected)
{
view.AddSubview(busyindicator);
NSTimer.CreateScheduledTimer(TimeSpan.FromSeconds(0.3), delegate
{
if (isDisposed)
return;
maps.Frame = new CGRect(Frame.Location.X, 0, Frame.Size.Width, Frame.Size.Height);
view.AddSubview(mainGrid);
});
}
else
{
label = new UILabel();
label.TextAlignment = UITextAlignment.Center;
label.Text = "Since this application requires network connection, " +
"Please check your network connection";
label.Font = UIFont.SystemFontOfSize(14);
label.Frame = new CGRect(0, 0, 300, 300);
label.TextColor = UIColor.Black;
label.LineBreakMode = UILineBreakMode.WordWrap;
label.Lines = 2;
view.AddSubview(label);
}
AddSubview(view);
}
async Task<bool> IsConnected()
{
var webRequest = (HttpWebRequest)WebRequest.Create("https://www.openstreetmap.org");
webRequest.Method = "HEAD";
try
{
using (var httpResponse = await webRequest.GetResponseAsync() as HttpWebResponse)
{
if (httpResponse != null && httpResponse.StatusCode == HttpStatusCode.OK)
return true;
return false;
}
}
catch (WebException)
{
return false;
}
}
void AddMainView()
{
if (childGrid == null)
childGrid = new UIView();
mainGrid = new UIView();
mainGrid.BackgroundColor = UIColor.FromRGB(243, 239, 233);
mainGrid.AddSubview(childGrid);
maps = new SFMap();
ImageryLayer layer = new ImageryLayer();
maps.Layers.Add(layer);
maps.Delegate = new OSMDelegate(this);
mainGrid.AddSubview(maps);
label1 = new UILabel();
label1.TextColor = UIColor.Black;
label1.LayoutMargins = new UIEdgeInsets(2, 2, 3, 2);
label1.Font = UIFont.FromName("Helvetica", 12);
var text = new NSString("©");
var stringAtribute = new NSDictionary(UIStringAttributeKey.Font, label1.Font,
UIStringAttributeKey.ForegroundColor, UIColor.Black);
UIStringAttributes strAtr = new UIStringAttributes(stringAtribute);
label1.Text = text;
label1Size = text.GetSizeUsingAttributes(strAtr);
label1.BackgroundColor = UIColor.White;
mainGrid.AddSubview(label1);
label2 = new UILabel();
label2.TextColor = UIColor.FromRGB(0, 212, 255);
label2.LayoutMargins = new UIEdgeInsets(1, 2, 3, 2);
label2.Font = UIFont.FromName("Helvetica", 12);
var text1 = new NSString("OpenStreetMap contributors.");
var stringAtribute1 = new NSDictionary(UIStringAttributeKey.Font, label2.Font,
UIStringAttributeKey.ForegroundColor, UIColor.FromRGB(0, 212, 255));
UIStringAttributes strAtr1 = new UIStringAttributes(stringAtribute);
label2.Text = text1;
label2Size = text1.GetSizeUsingAttributes(strAtr1);
label2.BackgroundColor = UIColor.White;
label2.UserInteractionEnabled = true;
UITapGestureRecognizer tapGesture = new UITapGestureRecognizer();
tapGesture.ShouldReceiveTouch += TapGesture_ShouldReceiveTouch;
label2.AddGestureRecognizer(tapGesture);
mainGrid.AddSubview(label2);
}
}
public class OSMDelegate : SFMapsDelegate
{
OSM sample;
public OSMDelegate(OSM osm)
{
sample = osm;
}
public override void DidLoad(SFMap map)
{
if (sample.busyindicator != null)
{
sample.busyindicator.RemoveFromSuperview();
sample.busyindicator = null;
}
}
}
}