How determine either location service is on or off?

 

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.

0001

001

  1: using System.Device.Location; // for GPS Service 
  2: //And
  3: using Microsoft.Phone.Shell;  // for PhoneApplicationService  current status
  4: 

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 GeoWatcher
 31:         {
 32:             get
 33:             {
 34:                 return geoWatcher;
 35:             }
 36:             set
 37:             {
 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 pages 
 70:                 allow = (result == MessageBoxResult.OK);
 71:                 PhoneApplicationService.Current.State["allow"] = allow;
 72:             }
 73:             return allow;
 74:         }

002

003

Here is source code

thank you for your time.

Happy Coding

Advertisements

About Pavan Pareta

My name is Pavan Pareta, I enjoy computers and general technology. I am more interested in the areas of Computer Security, Programming and developing innovative s/w for Windows Mobile and Windows Phone 7. I like MS Technology and support open-source software, because I believe it plays a major role in knowledge sharing and exchange of ideas, which effectively leads to better software. Additionally, I am a follower of the KISS (Keep It Simple Stupid) philosophy. I Love C#.NET and recently I am fall in love with Silverlight :) Introduce to my machine My main workstation is a Intel Pentium Core Duo and my primary operating system is Windows 7.
This entry was posted in Windows Mobile. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s