Windows Phone 7: Using GeoLocation API and Consuming Web-Service with C#

Hey! I am back with a third post on WP7 Development with C#. This time we’ll be making a Windows Phone 7.1 application that uses the GeoCoordinateWatcher class and consumes one web-service.

  1. Begin by launching Microsoft Visual Studio 2010 on your computer, click File, next New and select Project.
  2. From the Installed Templates Pane, expand Visual C# and select Silverlight for Windows Phone.
  3. From the Project Templates List in the middle select Windows Phone Application. Name the project whatever you want I named mine “geolocationsample” and click OK.

Microsoft Visual Studio 2010 will now generate all the project files for us to use. Before we continue, start by dropping two UI components from the Toolbox, one Textbox and one Button. For the button1 set the text to “Find Me” and save. Make sure it looks as shown in the screenshot below:



Next, right-click button1 and select Properties, from the Properties panel, select Events and locate the Click, once located double-click it so that Microsoft Visual Studio adds button1_Click event to our code. Leave it for now; we’re going to write code for that later.


To be able to use the GeoCoordinateWatcher class we need to add another assembly reference to our code, you do this from the Solution Explorer, by right-clicking References and selecting Add Reference. Find the System.Device reference and click OK.



Don’t forget to add the namespace inside the code as well.


Time to write code for the button1_Click event, you can just copy the code I’ve written below:

//Setup two variables
double latitude, longitude = 0.0;  
//Create an instance of the GeoCoordinateWatcher class
GeoCoordinateWatcher gwatcher = new GeoCoordinateWatcher();
//Store all the Position information inside PositionNow 
var PositionNow = gwatcher.Position;

   New York, United States
latitude = 74.0064;
longitude = 40.7142;

  If the location is NOT unknown 
  then retrieve Latitude and Longitude 
if (!PositionNow.Location.IsUnknown)
     latitude = PositionNow.Location.Latitude;
     longitude = PositionNow.Location.Longitude;

The above code will compile fine without syntax errors and/or other errors. However, for us to retrieve location information i.e. for example New York, United States latitude = 74.0064, and longitude = 40.7142, we have to consume some web-service that does this for us.

I’ve decided that I will use the Microsoft Research Maps (, the TerraService (, which provides an *.wsdl for Microsoft Visual Studio to read from XML,

Copy this url = In Microsoft Visual Studio, right-click the project (In my case geolocationsample), and from the context-menu select Add Service Reference.

(Click to view larger)

In my project, I left the namespace name ServiceReference1 and clicked OK.

Note: If Microsoft Visual Studio reports Error: Cannot import wsdl:binding, then you’ll have to reconfigure the Service Reference to fix this, else IntelliSense will not find the namespace of the service.

To fix this, simply right-click the Service ReferenceServiceReference1 and from the context-menu select Configure Service Reference.

Uncheck Reuse types in referenced assemblies and click OK.

(Click to view larger)

Now, let us go back to the button1_Click and consume the Web-Service we’ve referenced, copy the code below and paste it before the closing curly bracket (}) in the button1_Click event.

//Create an instance of the TerraServiceSoapClient class 
ServiceReference1.TerraServiceSoapClient tsclient = new ServiceReference1.TerraServiceSoapClient();

//Add an event handler
tsclient.ConvertLonLatPtToNearestPlaceCompleted += new EventHandler(tsclient_ConvertLonLatPtToNearestPlaceCompleted);

//Pass latitude and longitude and consume           
tsclient.ConvertLonLatPtToNearestPlaceAsync(new ServiceReference1.LonLatPt { Lat = latitude, Lon = longitude }); 

Since we’ve added another event handler (tsclient_ConvertLonLatPtToNearestPlaceCompleted), we need to write some code within it as well, copy the line below:

textBox1.Text = e.Result;

If you have done everything correctly, then you’re good to go, hit F5 to build and run in debug mode inside the Windows Phone Emulator.

In my Textbox it says: “7807 km NE of New York, New York, United States”, so it worked slightly with a fraction of error I guess, anyway.

To find more latitudes and longitudes, simply use, new york latitude longitude or Redmond, Washington, United StatesNote: Redmond, Washington, United States, latitude = 47.674 and longitude = -122.12, anyway, good luck hope this worked for you.

(Click image to view larger)

If you have problems with this code sample, simply do this, from the Solution Explorer right-click the ReferenceService1 and from the context-menu select Update Service Reference, this will re-download all the necessary information, code and more for your app to consume the web-service.

Download attached C# code sample:

Leave a Reply