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.


