In this post I am going to show how to determine programmatically location service, is on or off.
Why is it necessary in your application?
Let take an example while developing GPS based application for Windows Phone 7 and submitting app to market place, it is very important to get certified your application, user must know that your application going to use Windows Phone GPS service.
Here is the solution for the same case study.
Add the reference in your application.
1: using System.Device.Location; // for GPS Service2: //And3: using Microsoft.Phone.Shell; // for PhoneApplicationService current status4:
Initialization GeoCoordinateWatcher object for execute the GPS Service.
1: void MainPage_Loaded(object sender, RoutedEventArgs e)2: {3: geoWatcher = new GeoCoordinateWatcher();4: geoWatcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(geoWatcher_StatusChanged);5: geoWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(geoWatcher_PositionChanged);6:7:8: if (DoChk())9: {10: geoWatcher.Start();11: }12: }13:14: void geoWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)15: {16: textBlock1.Text = string.Format("Latitude:{0}\nLongitude:{1}", e.Position.Location.Latitude, e.Position.Location.Longitude);17: }18:19: void geoWatcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)20: {21: textBlock2.Text = string.Format("Status:{0}" , e.Status.ToString());22:23: if (DoChk())24: {25: geoWatcher.Start();26: }27:28: }29:30: public GeoCoordinateWatcher GeoWatcher31: {32: get33: {34: return geoWatcher;35: }36: set37: {38: if (geoWatcher != value)39: {40: geoWatcher = value;41: }42: if (geoWatcher.Status == GeoPositionStatus.Disabled)43: {44: geoWatcher.Stop();45: }46: }47:48: }49:50:51: private bool DoChk()52: {53: bool allow = false;54:55:56: if (PhoneApplicationService.Current.State.ContainsKey("allow"))57: {58: allow = (bool)PhoneApplicationService.Current.State["allow"];59: }60:61: if (allow == false)62: {63: var result = MessageBox.Show(64: "Application uses your Phone location. Do you wish " +65: "to give it permission to use your location?",66: "User Location Data",67: MessageBoxButton.OKCancel);68:69: // Save answer so you can access it on other pages70: allow = (result == MessageBoxResult.OK);71: PhoneApplicationService.Current.State["allow"] = allow;72: }73: return allow;74: }
Here is source code
thank you for your time.
Happy Coding
Post a Comment