The Windows Club

GPS, Location API and Calling Web Services: Windows Phone Apps Development Tutorial – 25

We learned about InputScopes in our last tutorial, as a part of the Windows Phone 7.5 Mango App development series. In this lesson we shall learn about the how to retrieve the global position of the phone,i.e., its latitude and longitude and then call a web service that will resolve the latitude and longitude into City, State and Country format.

The Windows Phone 7 is equipped with a GPS interface. When coupled with the Location Service API of the windows phone 7 we can use it to determine the latitude and longitude, i.e., the current position of the phone. We can then use these values of latitude and longitude to determine the city, state and country. We use a web service to resolve the city, state and country. A web service can be thought of as a method that runs over the internet. A webservice is hosted on a server and its method’s name is exposed along with the parameters that it will accept and what type of data it will return back. We can call such a web service over the internet, retrieve results and do something meaningful with the data obtained.

So let’s get hands on practical experience right away!

Create a new Windows Phone 7 project with a unique name like ‘GPSDemo’. Copy and paste the following xaml code in the Content Panel Grid.

<TextBlock Height=”30″

                         HorizontalAlignment=”Left”

                        Margin=”12,23,0,0″

                        Name=”textBlock1″

                        Text=””

                       VerticalAlignment=”Top”

                       Width=”423″ />

<Button Content=”Find Me”

                  Height=”72″

                  HorizontalAlignment=”Left”

                 Margin=”275,59,0,0″

                 Name=”button1″

                 VerticalAlignment=”Top”

                 Width=”160″

                 Click=”button1_Click” />

With the code given above we create a blank textblock and a button with the content property changed to Find Me. Navigate to the button1_Click event. Before we write the code for this even we need to add a reference to . Right click the title of the project in the solution explorer and select Add Reference from the menu. Under the ‘.Net’ tab scroll down and select the component name ‘System.Device’. Click OK to add the dll file to your project. Next type the following line of code after the last using statement in MainPage.xaml.cs.

using System.Device.Location;

Next we create a reference to the web service. To do this right click the project title and select Add Service Reference from the menu. Type the following URL ‘http://msrmaps.com/TerraService2.asmx’ in the Address text box and click ‘Go’. Once the web service is identified over the internet you will see a list of operations available under that web service. Simple change the namespace from ServiceReference1 to myTerraService and click the OK button. Visual Studio builds the proxy class that you will use to work with the web service. Once this is done you are all set to work with the GPS interface of the phone. Copy and paste the following lines of code in the button1_Click event.

GeoCoordinateWatcher myWatcher = new GeoCoordinateWatcher();

var myPosition = myWatcher.Position;

double latitude = 18.916;

double longitude = 72.9;

if (!myPosition.Location.IsUnknown)

{

      latitude = myPosition.Location.Latitude;

      longitude = myPosition.Location.Longitude;

}

myTerraService.TerraServiceSoapClient client = new myTerraService.TerraServiceSoapClient();

client.ConvertLonLatPtToNearestPlaceCompleted += new EventHandler<myTerraService.ConvertLonLatPtToNearestPlaceCompletedEventArgs>(client_ConvertLonLatPtToNearestPlaceCompleted);

client.ConvertLonLatPtToNearestPlaceAsync(new myTerraService.LonLatPt { Lat = latitude, Lon = longitude });

Copy and paste the following method after the last curly braces of the button1_Click method

void client_ConvertLonLatPtToNearestPlaceCompleted(object sender, myTerraService.ConvertLonLatPtToNearestPlaceCompletedEventArgs e)

{

       textBlock1.Text = e.Result;

}

Now let’s have a look at what exactly the code does. At first we get the values for the latitude and longitude by creating an object of FeoCoordinateWatcher class called myWatcher and then access the latitude and longitude simply by accessing the latitude and longitude property of the myPosition.Location. Next we create an asynchronous call to the web service passing the latitude and longitude. Finally we simply display the result of the webservice in the textblock. An asynchronous call is made so that the application remains responsive even while the result from the web service is being received.

To learn more about GPS, Location API and Calling Web Services visit www.msdn.com.

This concludes our Windows Phone 7.5 Mango App development series.

Links to all the parts of this tutorial series:

  1. Learn to Develop Windows Phone 7.5 Mango Applications: Part 1
  2. Developing Windows Phone 7.5 Mango Applications: Part 2 (HelloWorld app)
  3. Developing Windows Phone 7.5 Apps: Part 3; Variables, Data Types And Assigning Values
  4. Developing Windows 7.5 Mango Apps, Part 4: if statement
  5. Developing Windows 7.5 Mango Apps, Part 5: Operators, expressions, statements
  6. Developing Windows 7.5 Mango Apps, Part 6: Switch Statement
  7. Developing Windows Phone Mango Apps, Part 7: for iterations
  8. Using Simple Helper Methods while developing Windows Phone 7.5 Mango Apps: Part 8
  9. Working With Strings: Windows Phone 7.5 Mango Apps Development – Part 9
  10. Working With DateTime: Windows Phone 7.5 Mango Apps Development – Part 10
  11. Understanding and Creating Classes: Windows Phone 7.5 Mango Apps Development – Part 11
  12. Working With Classes: Windows Phone 7.5 Mango Apps Development – Part 12
  13. Understanding Namespaces: Windows Phone 7.5 Mango Apps Development – Part 13
  14. Collection of Objects: Windows Phone Apps Development Turorial – Part 14
  15. Understanding XAML: Windows Phone Apps Development Turorial – Part 15
  16. Silverlight Layout Controls: Windows Phone Apps Development Tutorial – Part 16
  17. XAML Event And Input Controls: Windows Phone Apps Development Tutorial – Part 17
  18. Working with the Image Control: Windows Phone Apps Development Tutorial – Part 18
  19. Working With Styles And Resources: Windows Phone Apps Development Tutorial – Part 19
  20. Apply style and theme resource to controls: Windows Phone Apps Development Tutorial – Part 20
  21. Navigating Between XAML Pages: Windows Phone Apps Development Tutorial – Part 21
  22. Working With The Application Bar: Windows Phone Apps Development Tutorial – Part 22
  23. Using Canvas as a Dialog: Windows Phone Apps Development Tutorial – Part 23
  24. Adding Different Inputscopes: Windows Phone Apps Development Tutorial – Part 24
  25. GPS, Location API and Calling Web Services: Windows Phone Apps Development Tutorial – 25.