// We need to perform a lot of checks to make sure location data and region monitoring are available and enabled. // For simplicity, we're logging errors in the console. public void StartMonitoringRegion (CLCircularRegion region, ROMPLocation rompLoc, string sessionKey, int locationID, string locationName) { if (CLLocationManager.LocationServicesEnabled) { if (CLLocationManager.Status != CLAuthorizationStatus.Denied) { if (CLLocationManager.IsMonitoringAvailable (typeof(CLCircularRegion))) { //LocMgr.DesiredAccuracy = 1; FenceMgr.DesiredAccuracy = CLLocation.AccuracyNearestTenMeters; if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) { FenceMgr.RequestAlwaysAuthorization (); } var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Badge, null); UIApplication.SharedApplication.RegisterUserNotificationSettings (settings); FenceMgr.RegionEntered += (o, e) => { rompLoc.CheckIn(sessionKey, locationID); Console.WriteLine ("Just entered " + e.Region.ToString ()); UILocalNotification notification = new UILocalNotification(); DateTime date = DateTime.Now.AddSeconds(5.0); date = date.ToLocalTime(); NSDate nsDate = (NSDate)DateTime.SpecifyKind(date, DateTimeKind.Utc); notification.FireDate = nsDate; notification.AlertAction = "Check In"; notification.AlertBody = "Successfully Checked Into " + locationName; UIApplication.SharedApplication.ScheduleLocalNotification(notification); RegionEntered (this, new RegionChangedEventArgs ((CLCircularRegion)e.Region)); }; FenceMgr.RegionLeft += (o, e) => { rompLoc.CheckOut(sessionKey, locationID); Console.WriteLine ("Just left " + e.Region.ToString ()); UILocalNotification notification = new UILocalNotification(); DateTime date = DateTime.Now.AddSeconds(5.0); date = date.ToLocalTime(); NSDate nsDate = (NSDate)DateTime.SpecifyKind(date, DateTimeKind.Utc); notification.FireDate = nsDate; notification.AlertAction = "Check Out"; notification.AlertBody = "Successfully Checked Out Of " + locationName; UIApplication.SharedApplication.ScheduleLocalNotification(notification); RegionLeft (this, new RegionChangedEventArgs ((CLCircularRegion)e.Region)); }; FenceMgr.DidStartMonitoringForRegion += (o, e) => { Console.WriteLine ("Now monitoring region {0}", e.Region.ToString ()); }; FenceMgr.StartMonitoring (region); } else { Console.WriteLine ("This app requires region monitoring, which is unavailable on this device"); } } else { Console.WriteLine ("App is not authorized to use location data"); } // Get some output from our manager in case of failure FenceMgr.Failed += (o, e) => { Console.WriteLine (e.Error); }; } else { //Let the user know that they need to enable LocationServices Console.WriteLine ("Location services not enabled, please enable this in your Settings"); } }
public override void ViewDidLoad() { base.ViewDidLoad (); var locSvc = new ROMPLocation (); myFacilities = locSvc.GetLocations (sessionKey, groupID); locMan = new CLLocationManager (); if (myFacilities.Count () > 0) { if (!CLLocationManager.LocationServicesEnabled) { UIAlertView _error = new UIAlertView ("Error", "Location services not enabled, please enable this in your Settings.", null, "Ok", null); _error.Show (); } else if (CLLocationManager.Status == CLAuthorizationStatus.Denied) { UIAlertView _error = new UIAlertView ("Error", "App is not authorized to use location data.", null, "Ok", null); _error.Show (); } else { //locMan.LocationUpdated += LocationChanged; locMan.DesiredAccuracy = CLLocation.AccuracyNearestTenMeters; if (UIDevice.CurrentDevice.CheckSystemVersion (6, 0)) { locMan.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => { UpdateLocation (e.Locations [e.Locations.Length - 1]); }; } else { #pragma warning disable 618 // this won't be called on iOS 6 (deprecated) locMan.UpdatedLocation += (object sender, CLLocationUpdatedEventArgs e) => { UpdateLocation (e.NewLocation); }; #pragma warning restore 618 } if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) { locMan.RequestAlwaysAuthorization (); } if (CLLocationManager.LocationServicesEnabled) locMan.StartUpdatingLocation (); if (CLLocationManager.HeadingAvailable) locMan.StartUpdatingHeading (); } btnCheckIn.BackgroundColor = new UIColor(new CoreGraphics.CGColor(0.9f,0.9f,0.9f)); btnCheckIn.Layer.CornerRadius = 1; btnCheckIn.TouchUpInside += (object sender, EventArgs e) => { if (currentLocation != null) { if (groupID <= 2) { string absResult = "You Are Not Within A Specified Zone."; string absTitle = "Error"; if (btnCheckIn.Title (UIControlState.Normal) == "Check In") { foreach (FacilityCoordinates fc in myFacilities) { var fenceLat = fc.Latitude; var fenceLon = fc.Longitude; var R = 6371; // Radius of the earth in km var dLat = deg2rad (currentLocation.Coordinate.Latitude - fenceLat); // deg2rad below var dLon = deg2rad (currentLocation.Coordinate.Longitude - fenceLon); var a = Math.Sin (dLat / 2) * Math.Sin (dLat / 2) + Math.Cos (deg2rad (fenceLat)) * Math.Cos (deg2rad (currentLocation.Coordinate.Latitude)) * Math.Sin (dLon / 2) * Math.Sin (dLon / 2); var c = 2 * Math.Atan2 (Math.Sqrt (a), Math.Sqrt (1 - a)); var d = R * c; if (d <= 0.25) { string result = locSvc.CheckIn (sessionKey, fc.LocationID); if (result == "Success") { absTitle = "Success"; absResult = "Check In Successful"; btnCheckIn.SetTitle ("CHECK OUT", UIControlState.Normal); break; } else { absTitle = "Error"; absResult = "An Unexpected Error Occurred. Try Again"; } } } } else if (btnCheckIn.Title (UIControlState.Normal) == "Check Out") { string result = locSvc.CheckOutWithoutLocation (sessionKey); if (result == "Success") { absTitle = "Success"; absResult = "Check Out Successful"; btnCheckIn.SetTitle ("CHECK IN", UIControlState.Normal); } else { absTitle = "Error"; absResult = "An Unexpected Error Occurred. Try Again"; } } UIAlertView _error = new UIAlertView (absTitle, absResult, null, "Ok", null); _error.Show (); } else if (groupID > 2 && groupID <= 7) { string absResult = ""; string absTitle = ""; if (btnCheckIn.Title (UIControlState.Normal) == "Check In") { string result = locSvc.CheckInWithLocation (sessionKey, -1, currentLocation.Coordinate.Latitude, currentLocation.Coordinate.Longitude); if (result == "Success") { absTitle = "Success"; absResult = "Check In Successful"; btnCheckIn.SetTitle ("CHECK OUT", UIControlState.Normal); } else { absTitle = "Error"; absResult = "An Unexpected Error Occurred. Try Again"; } } else if (btnCheckIn.Title (UIControlState.Normal) == "Check Out") { string result = locSvc.CheckOutWithoutLocation (sessionKey); if (result == "Success") { absTitle = "Success"; absResult = "Check In Successful"; btnCheckIn.SetTitle ("CHECK IN", UIControlState.Normal); } else { absTitle = "Error"; absResult = "An Unexpected Error Occurred. Try Again"; } } UIAlertView _error = new UIAlertView (absTitle, absResult, null, "Ok", null); _error.Show (); } else if (groupID == 8) { var fenceLat = 48.46003187; var fenceLon = -89.18908003; var R = 6371; // Radius of the earth in km var dLat = deg2rad (currentLocation.Coordinate.Latitude - fenceLat); // deg2rad below var dLon = deg2rad (currentLocation.Coordinate.Longitude - fenceLon); var a = Math.Sin (dLat / 2) * Math.Sin (dLat / 2) + Math.Cos (deg2rad (fenceLat)) * Math.Cos (deg2rad (currentLocation.Coordinate.Latitude)) * Math.Sin (dLon / 2) * Math.Sin (dLon / 2); var c = 2 * Math.Atan2 (Math.Sqrt (a), Math.Sqrt (1 - a)); var d = R * c; string absResult = d.ToString (); UIAlertView _error = new UIAlertView ("Check In", absResult, null, "Ok", null); _error.Show (); } } }; } else { btnCheckIn.Hidden = true; lblText.Text = "You have no locations to check in to. Please start the application during a rotation to properly utilize the functionality. Thank you."; } btnExit.BackgroundColor = new UIColor(new CoreGraphics.CGColor(0.9f,0.9f,0.9f)); btnExit.Layer.CornerRadius = 1; btnExit.TouchUpInside += (object sender, EventArgs e) => { Environment.Exit(0); }; }