Viewers: in countries Watching now:
Discover how to build professional apps that encompass the 17 major feature areas of Windows Phone, from XAML to multimedia to network access. In this course, author Michael Lehman details the standard hardware device configuration, teaches how to navigate the development environment, and explores the Windows Phone APIs. The course shows how to build sample applications while learning Windows Phone concepts and frameworks, including the Metro design language.
Now let's take a look at the code behind the GPS portion of this app. We have five controls on the screen here. The first is a button named, start tracking location, and we have three text blocks here for the longitude, latitude and accuracy. Because when you ask for location, you say how much accuracy you want. Like I want my location to be within say five meters. Because location services can end up returning you numbers based on GPS radio or WiFi or some combination of both.
It may actually give you back an accuracy that is either more accurate or less accurate than you actually requested. And then we finally have a map it button, which allows us to invoke the map test. Based on the data that we got from the longitude and latitude requests from the GPS. So let's go look at the code. In order to access location services we need System.Device.Location and Windows.Devices.Geolocation. In order to access the map we need to use Microsoft.Phone.Task.
The thing which gets us the location is something called a Geolocator. So, here is the declaration of our Geolocator object and the location longitude, latitude and accuracy come back in something called a Geoposition. So, here we allocate our Geoposition object. We come into the page, we allocate a new Geolocator and set our desired accuracy to ten meters. We look at the start tracking location method here in button1_Click. What we do is disable our existing buttons, so the user can't tap them multiple times.
We set the longitude and latitude text blocks to the word locating with an ellipsis at the end, and then we attempt to go get the current location. In order to do that we set up two timespans. There's an API called GetGeopositionAsync, that takes two timespan objects. The first one max age is how old of a GPS location are we willing to accept. In a real world situation, you're going to want to typically set this to maybe one minute or five minutes. Because you don't necessarily have to have the absolutely precise up-to-date geolocation.
If you do that, it can be heavy on battery usage. We've set it for the demo here to five seconds so that, as we click on the map, we can get immediate feedback. In real life, you probably want to set this to a timespan from minutes of one, two, three, four, five. Timeout has to do with how long between the time that you ask the GPS radio for a synch and when it's going to time out and say, hey I can't find the satellite. Thirty seconds is a real world reasonable number. GetGeopositionASync is a new Windows 8 style WinRT asynchronistic API.
We're going to take one step backwards here and look at the fact that we declared this method private async void. The reason we did that is so that we could come down here and say currentLoc equals await gl.GetGeopositionAsync. This is a much simpler solution than the one you had to use for Windows Phone 7, in which you had to set up your own custom event handlers. And deal with the callbacks. Once we get back to our location, we take our currentLoc which is a geoposition and we ask for a Coordinate, Latitude and then we turn it into a string. Similarly we do the same things for Longitude and Accuracy. And now that we have a geoposition we enable our map it button. Now you noticed we didn't check to see whether or not the location was valid. That's because the way GetGeopositionAsync works is that it will throw an exception if it can't get a location for you. So we have two separate exception handlers here. We have a catch for Unauthorized Exception, which is what happens when the user has disabled Access to location services.
And we have a regular kind of exception which is what happens when you can't get a GPS fix. And finally, in our try catch finally, we reset our go button to true so you can request a location over and over again. So that's how it happens that we update the text blocks on the screen with the longitude, latitude, and accuracy. Finally, let's take a look at how we map things. Down here in our button2_Click handler, we have something simply that says, do we have a geoposition object in currentLoc? If we have one, creating a map is as simple as instantiating a map task. Setting the center point into the map equal to a geocoordinate and passing in the latitude and the longitude, like this.
The zoom Level is anywhere from 1 to 20. 1 represents vision of the entire world and 20 represents the highest accuracy you can get, like 5 feet in front of a building. Somewhere between 10 and 12 is a reasonable size for navigating around the city in terms of something similar to a map you would have on paper. And once you set that up, the last thing you have to do is simply say map test dot show and the operating system handles everything else for you. Once again, this is way simpler than the way it was in Windows Phone 7 where you had to use big maps and set up things. And either host a web browser or use a seperate browser app. This is something the system handles for you automatically. Next up, let's take a look at how the accelerometer code works.
Find answers to the most frequently asked questions about Windows Phone SDK Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.