Geo Location Service in Windows Phone 8

Introduction

With the change of Windows Phone 7.5 to Windows Phone 8 API we have many changes in terms of layout, architecture and bug fixes, but there are many changes regarding the Geo Location classes. With Window Phone 8 SDK we have improvised classes that does the same job and is easy to use.

Agenda

  • Create a Demo Layout
  • Add using Windows.Devices.GeoLocation in CS file
  • Set geo location finder instance
  • And finally, set the Geo Position instance that returns the value of Latitude and Longitude.

Procedures

Step 1

Create a Blank Windows Phone Silverlight Project and try to mock the following layout.

layout of Windows Phone Silverlight

And, enable ID_CAP_LOCATION the Capability in Properties > WMAppManifest.xml as in the following:

application UI of Windows phone

Step 2

Now, we need to define the Geo Locator.

So, create a callback method for the Button's click event. //SetUp GeoLocator  

  1. Geolocator locationFinder = new Geolocator  
  2. {  
  3. DesiredAccuracyInMeters=50,  
  4. DesiredAccuracy=PositionAccuracy.Default  
  5. }; 

Here, we created an instance of the Geolocator class where we set the two fields, in other words DesiredAccuracyInMeter and DesiredAccuracy.

Step 3

Next, we need to define the Geopositon class that will provide the value of Latitude and Longitude. try  

  1. {  
  2. Geoposition currentLocation = await locationFinder.GetGeopositionAsync(maximumAge: TimeSpan.FromSeconds(120), timeout: TimeSpan.FromSeconds(10));  
  3.   
  4. longitude = currentLocation.Coordinate.Longitude.ToString("0.00");  
  5. latitude = currentLocation.Coordinate.Latitude.ToString("0.00");  
  6.   
  7. // Asign to Textboxes  
  8. tbLongitude.Text = longitude;  
  9. tbLatitude.Text = latitude;  
  10. }  
  11.   
  12. catch(Exception ex)  
  13. {  
  14.   
  15. MessageBox.Show("ERROR :"+ex.Message.ToString());  
  16.   
  17. }  
  18.   
  19. Here, we have initialized Geoposition instance with two arguments (maximum age and timeout).  
  20.   
  21. While going all this, you need some fields which must be global.  
  22.   
  23. // Fields  
  24. string longitude;  
  25. string latitude; 

The final code will look like:

  1. private async void btnGeoLocation_Click(object sender, RoutedEventArgs e)  
  2. {  
  3. //Genarte Geo Location  
  4.   
  5. //SetUp  
  6. Geolocator locationFinder = new Geolocator  
  7. {  
  8. DesiredAccuracyInMeters=50,  
  9. DesiredAccuracy=PositionAccuracy.Default  
  10. };  
  11.   
  12. // Try to Get The Postion  
  13. try  
  14. {  
  15. Geoposition currentLocation = await locationFinder.GetGeopositionAsync(maximumAge: TimeSpan.FromSeconds(120), timeout: TimeSpan.FromSeconds(10));  
  16.   
  17. longitude = currentLocation.Coordinate.Longitude.ToString("0.00");  
  18. latitude = currentLocation.Coordinate.Latitude.ToString("0.00");  
  19.   
  20. // Asign to Textboxes  
  21. tbLongitude.Text = longitude;  
  22. tbLatitude.Text = latitude;  
  23. }  
  24. catch(Exception ex)  
  25. {  
  26. MessageBox.Show("ERROR :"+ex.Message.ToString());  
  27. }  
  28.   

Output

After doing all this, we will get something like this.

Geo Location Service in Windows Phone

Conclusion

In this demo app we have done something old that we have done earlier in Windows Phone 7.5 SDK, but this time we have used Windows Phone 8 SDK's class that let us do the same job in a few lines of code. Next, we will try to put this latitude-Longitude in a map.

For this demo, if you experience any issue then try to figure it out from the enclosed solution file.

Up Next
    Ebook Download
    View all
    Learn
    View all