Part 25: Working with the Geolocator and Geoposition Classes
In the previous lesson I was able to hardcode a GeoCoordinate object and pass it to the Map control to set it's position. However, for our app, we will want to retrieve that information from the phone. The Windows Phone API has a Geolocator class that will use the phone's GPS system to determine where in the world it is.
Here's the game plan:
- We'll experiment with our UpdateMap() method to learn about the Geolocator class, how to set it's accuracy and obtain the current position.
- We'll learn about other classes in the Windows Phone API that allow us to work with the map and the locale of the user
- We'll configure the Windows Phone Emulator and set its position to a specific place, namely, the John Hancock Center in Chicago. I have a special affinity for Chicago and for that building specifically that I'll tell you about later.
1. Modify UpdateMap() to retrieve it's position from the Geolocator class
We'll pick up where we left off in the previous lesson, updating the UpdateMap() method as follows:
- We create a new instance of the Geolocator class and set it's DesiredAccuracyInMeters property to 50 (meters).
- We call the GetGeopositionAsync method. At this point, I'd like to point out the best article I read on the process of acquiring the Geoposition of the phone and why there are several factors to consider. Actually, It's a blog post on the Windows Phone Developer blog from Daniel Estrada Alva, a software development engineer on the Windows Phone team. http://blogs.windows.com/windows_phone/b/wpdev/archive/2012/11/30/acquiring-a-single-geoposition-in-windows-phone-8.aspx
- There's a keyword you may not be familiar with in line 40: the await keyword. This corresponds with the async keyword I added to the method's signature in line 34. Just how this works and why I would like to save until a later lesson when I take extensively about the new await functionality in C# 5.0. For now, just understand that the purpose of this is to keep our application responsive while potentially long running tasks are executing. In this case, the phone may take a long time to acquire the current location using its built in GPS hardware. While it is acquiring the location, the user's phone, and even our app, should continue to be responsive to the user's input. Again, more later.
- The Geoposition object has a collection of coordinates. However, we have to do a little conversion because the Map control requires those coordinates be in a GeoCoordinate object.
- Finally, we pass the GeoCoordinate we just created to the SetView() method.
When we run the app (F5), we can see the result:
... the result is that the Map control is zoomed into Redmond, Washing, the home of Microsoft's main campus. This is the default position of the Windows Phone Emulator.
2. Use the Emulator's Additional Tools to change the virtual location of the Emulator for testing
So, how do we change the Emulator to use a different latitude and longitude?
- I click the double chevron to open up the Additional Tools panel.
- I click the Location tab at the top.
- I the words "John Hancock, Chicago" into the Search textbox and hit the Enter key on my keyboard.
This shows me a different map ... a map of downtown Chicago positioned at the John Hancock Center. Excellent.
I'll use the primary (left) mouse button to add a pin on that location.
By adding a pin, I've set the Current Location to the correct latitude and longitude for the John Hancock Center.
However, sometimes it takes a while for the phone's GPS to catch up and refresh. What I've found works best is to exit out of the AroundMe app, go to the Windows Phone 8 application list, open Maps and wait for the phone to reposition to the desired location. When I do that, after about 10 seconds, the Maps program repositions to downtown Chicago.
Now, I re-run the app from the Application List (I don't need to stop and re-start the app from Visual Studio) and it should pick up the change in GeoPosition.
Just to recap, the big take away from this lesson is how to use the Geolocator class to interface with the phone's GPS hardware to retrieve an instance of the GeoPosition class. The GeoPosition class has a number of interesting details, but for our purposes we care only about the longitude and latitude, which we can use to construct a new GeoCoordinate class in order to center the Map control. We learned how to configure the Phone Emulator's Location to make it think it is at a specific place in the world to test various location scenarios.