Created
January 12, 2012 21:40
-
-
Save anujb/1603316 to your computer and use it in GitHub Desktop.
LocationManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LocationManager | |
{ | |
public static CLLocationManager CoreLocationManager { get; set; } | |
public static CLLocation OldLocation { get; private set; } | |
public static CLLocation NewLocation { get; private set; } | |
public static CLHeading NewHeading { get; private set; } | |
static Action<CLLocationUpdatedEventArgs> _LocationUpdated; | |
public static Action<CLLocationUpdatedEventArgs> LocationUpdated { | |
get { return _LocationUpdated; } | |
set | |
{ | |
//Null and Duplicate Subscriber Check | |
if(value != null && _LocationUpdated.GetInvocationList().Contains(value) == false) | |
{ | |
_LocationUpdated += value; | |
} | |
} | |
} | |
static Action<CLHeading> _HeadingUpdated; | |
public static Action<CLHeading> HeadingUpdated { | |
get { return _HeadingUpdated; } | |
set | |
{ | |
//Null and Duplicate Subscriber Check | |
if(value != null && _HeadingUpdated.GetInvocationList().Contains(value) == false) | |
{ | |
_HeadingUpdated += value; | |
} | |
} | |
} | |
private static NSTimer UpdateLocationTimer; | |
private static NSTimer UpdateHeadingTimer; | |
public LocationManager() | |
{ | |
Initialize(); | |
} | |
public static void Initialize() | |
{ | |
CoreLocationManager = new CLLocationManager(); | |
CoreLocationManager.StartUpdatingLocation(); | |
CoreLocationManager.UpdatedHeading += HandleCoreLocationManagerUpdatedHeading; | |
CoreLocationManager.UpdatedLocation += HandleCoreLocationManagerUpdatedLocation; | |
UpdateLocationTimer = NSTimer.CreateRepeatingTimer(TimeSpan.FromMinutes(1), InitializeLocationUpdate); | |
UpdateHeadingTimer =NSTimer.CreateRepeatingTimer(TimeSpan.FromMinutes(1), InitializeHeadingUpdate); | |
} | |
public static void InitializeLocationUpdate() | |
{ | |
CoreLocationManager.StartUpdatingLocation(); | |
} | |
public static void InitializeHeadingUpdate() | |
{ | |
CoreLocationManager.StartUpdatingHeading(); | |
} | |
static void HandleCoreLocationManagerUpdatedLocation (object sender, CLLocationUpdatedEventArgs e) | |
{ | |
OldLocation = e.OldLocation; | |
NewLocation = e.NewLocation; | |
if(LocationUpdated != null) | |
LocationUpdated(e); | |
} | |
static void HandleCoreLocationManagerUpdatedHeading (object sender, CLHeadingUpdatedEventArgs e) | |
{ | |
NewHeading = e.NewHeading; | |
if(HeadingUpdated != null) | |
HeadingUpdated(e.NewHeading); | |
} | |
public static void RemoveSubscribers() | |
{ | |
_LocationUpdated = null; | |
_HeadingUpdated = null; | |
} | |
public static void Dispose() | |
{ | |
LocationUpdated = null; | |
OldLocation = null; | |
NewLocation = null; | |
NewHeading = null; | |
UpdateHeadingTimer.Invalidate(); | |
UpdateLocationTimer.Invalidate(); | |
CoreLocationManager.StopUpdatingHeading(); | |
CoreLocationManager.StopUpdatingLocation(); | |
CoreLocationManager.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment