-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapViewController.cs
executable file
·269 lines (207 loc) · 7.89 KB
/
MapViewController.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using UIKit;
using CoreLocation;
using MapKit;
using Foundation;
using System.Collections.Generic;
using Xamarin.Forms.Maps;
namespace MapDemo
{
partial class MapViewController : UIViewController
{
MyMapDelegate mapDel;
UISearchController searchController;
CLLocationManager locationManager = new CLLocationManager ();
bool fuera = true;
public MapViewController (IntPtr handle) : base (handle)
{
}
public void check(double lon, double lat) {
var mapCenter = new CLLocationCoordinate2D(lat, lon);
var mapRegion = MKCoordinateRegion.FromDistance(mapCenter, 3000, 3000);
map.CenterCoordinate = mapCenter;
map.Region = mapRegion;
if ((lon <= ((-34 * 0.0005) - 106.48694)) && (lat >= ((15 * 0.0005) + 31.739444)) && fuera) {
fuera = false;
var notification = new UILocalNotification();
// set the fire date (the date time in which it will fire)
notification.FireDate = NSDate.Now.AddSeconds(5); //DateTime.Now.AddSeconds(10));
notification.TimeZone = NSTimeZone.DefaultTimeZone;
// configure the alert stuff
notification.AlertTitle = "Peligro";
notification.AlertAction = "Alert Action";
notification.AlertBody = "¡Has entrado en una zona de peligro!";
notification.UserInfo = NSDictionary.FromObjectAndKey(new NSString("UserInfo for notification"), new NSString("Notification"));
notification.SoundName = UILocalNotification.DefaultSoundName;
// schedule it
UIApplication.SharedApplication.ScheduleLocalNotification(notification);
}
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
locationManager.RequestWhenInUseAuthorization ();
// set map type and show user location
map.MapType = MKMapType.Standard;
map.ShowsUserLocation = true;
map.Bounds = View.Bounds;
// set map center and region
double lat = 31.739444;
double lon = -106.48694;
var mapCenter = new CLLocationCoordinate2D (lat, lon);
var mapRegion = MKCoordinateRegion.FromDistance (mapCenter, 3000, 3000);
map.CenterCoordinate = mapCenter;
map.Region = mapRegion;
// add an annotation
var actualAnnotation = new MKPointAnnotation();
actualAnnotation.Title = "Ubicacion Actual";
actualAnnotation.Coordinate = new CLLocationCoordinate2D(lat, lon);
map.AddAnnotation(actualAnnotation);
// set the map delegate
mapDel = new MyMapDelegate();
map.Delegate = mapDel;
// add an overlay
var circleOverlay = MKCircle.Circle(mapCenter, 200);
map.AddOverlay(circleOverlay);
// Safe Zone
var mapSafe = new CLLocationCoordinate2D(31.75, -106.5);
var safeOverlay_01 = MKCircle.Circle(mapSafe, 20);
map.AddOverlay(safeOverlay_01);
//map.AddAnnotation(new MonkeyAnnotation("Xamarin", mapCenter));
var punto = new MKPointAnnotation();
punto.Title = "Startbucks";
punto.Coordinate = new CLLocationCoordinate2D(31.75, -106.5);
map.AddAnnotation(punto);
// Danger Zone
var mapDanger = new CLLocationCoordinate2D(31.7526, -106.5053);
var dangerOverlay_01 = MKCircle.Circle(mapDanger, 400);
map.AddOverlay(dangerOverlay_01);
// Busqueda
var searchResultsController = new SearchResultsViewController (map);
var searchUpdater = new SearchResultsUpdator ();
searchUpdater.UpdateSearchResults += searchResultsController.Search;
//add the search controller
searchController = new UISearchController (searchResultsController) {
SearchResultsUpdater = searchUpdater
};
searchController.SearchBar.SizeToFit ();
searchController.SearchBar.SearchBarStyle = UISearchBarStyle.Minimal;
searchController.SearchBar.Placeholder = "Enter a search query";
searchController.HidesNavigationBarDuringPresentation = false;
NavigationItem.TitleView = searchController.SearchBar;
DefinesPresentationContext = true;
/* Button Actions */
upButton.TouchUpInside += (object sender, EventArgs e) => {
map.RemoveAnnotation(actualAnnotation);
map.RemoveOverlay(circleOverlay);
lat = lat + 0.0005;
actualAnnotation.Title = "Ubicacion Actual";
actualAnnotation.Coordinate = new CLLocationCoordinate2D(lat, lon);
map.AddAnnotation(actualAnnotation);
mapCenter = new CLLocationCoordinate2D(lat, lon);
circleOverlay = MKCircle.Circle(mapCenter, 200);
map.AddOverlay(circleOverlay);
check(lon, lat);
};
leftButton.TouchUpInside += (object sender, EventArgs e) => {
map.RemoveAnnotation(actualAnnotation);
map.RemoveOverlay(circleOverlay);
lon = lon - 0.0005;
actualAnnotation.Title = "Ubicacion Actual";
actualAnnotation.Coordinate = new CLLocationCoordinate2D(lat, lon);
map.AddAnnotation(actualAnnotation);
mapCenter = new CLLocationCoordinate2D(lat, lon);
circleOverlay = MKCircle.Circle(mapCenter, 200);
map.AddOverlay(circleOverlay);
check(lon, lat);
};
rightButton.TouchUpInside += (object sender, EventArgs e) =>
{
map.RemoveAnnotation(actualAnnotation);
map.RemoveOverlay(circleOverlay);
lon = lon + 0.0005;
actualAnnotation.Title = "Ubicacion Actual";
actualAnnotation.Coordinate = new CLLocationCoordinate2D(lat, lon);
map.AddAnnotation(actualAnnotation);
mapCenter = new CLLocationCoordinate2D(lat, lon);
circleOverlay = MKCircle.Circle(mapCenter, 200);
map.AddOverlay(circleOverlay);
check(lon, lat);
};
downButton.TouchUpInside += (object sender, EventArgs e) =>
{
map.RemoveAnnotation(actualAnnotation);
map.RemoveOverlay(circleOverlay);
lat = lat - 0.0005;
actualAnnotation.Title = "Ubicacion Actual";
actualAnnotation.Coordinate = new CLLocationCoordinate2D(lat, lon);
map.AddAnnotation(actualAnnotation);
mapCenter = new CLLocationCoordinate2D(lat, lon);
circleOverlay = MKCircle.Circle(mapCenter, 200);
map.AddOverlay(circleOverlay);
check(lon, lat);
};
}
public class SearchResultsUpdator : UISearchResultsUpdating
{
public event Action<string> UpdateSearchResults = delegate {};
public override void UpdateSearchResultsForSearchController (UISearchController searchController)
{
this.UpdateSearchResults (searchController.SearchBar.Text);
}
}
public class CustomMap : Map
{
public List<Position> RouteCoordinates { get; set; }
public CustomMap()
{
RouteCoordinates = new List<Position>();
}
}
class MyMapDelegate : MKMapViewDelegate
{
string pId = "PinAnnotation";
string mId = "MonkeyAnnotation";
public override MKAnnotationView GetViewForAnnotation (MKMapView mapView, IMKAnnotation annotation)
{
MKAnnotationView anView;
if (annotation is MKUserLocation)
return null;
if (annotation is MonkeyAnnotation) {
// show monkey annotation
anView = mapView.DequeueReusableAnnotation (mId);
if (anView == null)
anView = new MKAnnotationView (annotation, mId);
anView.Image = UIImage.FromFile ("monkey.png");
anView.CanShowCallout = true;
anView.Draggable = true;
anView.RightCalloutAccessoryView = UIButton.FromType (UIButtonType.DetailDisclosure);
} else {
// show pin annotation
anView = (MKPinAnnotationView)mapView.DequeueReusableAnnotation (pId);
if (anView == null)
anView = new MKPinAnnotationView (annotation, pId);
((MKPinAnnotationView)anView).PinColor = MKPinAnnotationColor.Red;
anView.CanShowCallout = true;
}
return anView;
}
public override void CalloutAccessoryControlTapped (MKMapView mapView, MKAnnotationView view, UIControl control)
{
var monkeyAn = view.Annotation as MonkeyAnnotation;
if (monkeyAn != null) {
var alert = new UIAlertView ("Monkey Annotation", monkeyAn.Title, null, "OK");
alert.Show ();
}
}
public override MKOverlayView GetViewForOverlay (MKMapView mapView, IMKOverlay overlay)
{
var circleOverlay = overlay as MKCircle;
var circleView = new MKCircleView (circleOverlay);
circleView.FillColor = UIColor.Red;
circleView.Alpha = 0.4f;
return circleView;
}
}
}
}