By tying together a user’s location to the business logic of your app, you can easily provide app users with weather, coupons, sporting events, or concert listings for their current location. You can use the geolocation capabilities of Windows 8, along with the Bing Maps SDK, to create great location aware Windows Store apps.
Let’s take a look at how your app can use the location service to provide users location-specific info with either an IP address, WiFi network, or GPS data. Once you have the location, you can use the Bing Maps SDK to show the user where they are.
The Windows.Devices.Geolocation namespace is used for retrieving your location. Whether you use GPS, a WiFi network, or an IP address to retrieve a user’s location depends on two primary factors:
- The accuracy level requested. This is the ideal level of accuracy to meet your app’s needs.
- The availability of location data. ; Meaning, not all devices have GPS built in. ; ; Or a network or Wi-Fi connection may not be available when the location is requested.
When requesting a location, your app should specify your desired accuracy so the location data can be returned as quickly as possible within the given range. ; Each of the location types have degrees of accuracy:
- GPS – GPS provides the most accuracy, down to approximately 10 meters. ; However, if the GPS sensor needs to ‘wake up’ and acquire satellite data and then triangulate the data, it can take a minute or two to return the location details. But, once the GPS has acquired satellite connections, it can then acquire the next location point more quickly. ; Of course, the more the GPS is used, the more power it consumes. The Geolocator.DesiredAccuracy property must be set to High to enable to GPS to acquire data.
- Wi-Fi – A Wi-Fi connection can provide accuracy between 300-500 meters. ; If the connection exists, the location data can be returned faster than a GPS can acquire satellite connections. Set the DesiredAccuracy property to Default in order to acquire the location with Wi-Fi.
- IP – IP level resolution can bring accuracy down to about the 1 kilometer level. ; If the network connection exists, the location data can be returned faster than a GPS can acquire a satellite connection. You also set the DesiredAccuracy property to Default to acquire the location with IP. The location service returns the highest accuracy it can and lets you know how accurate it is in the Geocoordinate.Accuracy property.
Consider your desired accuracy value carefully. It can affect your app’s performance and power consumption. ; For example, if your app is providing a weather forecast, you rarely need the high accuracy of GPS. ; Also be sure to indicate the accuracy of your data by displaying an error radius based on the accuracy of the data returned. Here is the C# code to set your desired accuracy:
1 Geolocator geolocator = <span>new</span> Geolocator();<br />geolocator.DesiredAccuracy = Windows.Devices.Geolocation.PositionAccuracy.High;<br />
No matter what accuracy you want, the call to get your location is:
1 Geoposition pos = await geolocator.GetGeopositionAsync().AsTask(token);
1 <span>var</span> geolocator = Windows.Devices.Geolocation.Geolocator();<br />geolocator.desiredAccuracy =<br />Windows.Devices.Geolocation.PositionAccuracy.high;<br /><br /><span>// Get the geoposition, capturing the request in a 'promise' object.</span><br /><span>var</span> promise = geolocator.getGeopositionAsync();<br />
Remember that if you set your desired accuracy to High, you can’t be assured that the user’s device will be able to return high accuracy. Their PC may not have a GPS or it may be turned off or they may not be connected to Wi-Fi, etc. Always be prepared to gracefully handle these instances of lower accuracy data. How you do this depends on the functionality of your app. Can you continue with lower accuracy? Can you require GPS level accuracy? See the Guidelines for location-aware apps for more info.
The Bing Maps app is a good example of an app that adapts to the accuracy of the location data available to it. ; On a desktop PC, which typically doesn’t have GPS or Wi-Fi, it finds the location based on the IP address and zooms in to a region level when mapping the current location. ; On a laptop, which typically has a wireless card, it uses the Wi-Fi to get a more precise location and zoom in further when showing current location. ; If the user zooms in even further, the current location icon will show an error radius. ; On a device with GPS, Bing Maps gets the high accuracy location data from the GPS and displays a more accurate location.
As mentioned above, an app like the Bing Weather app only needs region information. ; Because of this, it can specify that it only needs low accuracy allowing it to quickly get a location value back. ; This makes the app more responsive to the user. ; Other examples of apps that may need only region level information include radio station guides, shopping deals, or local news feeds.
Displaying location data with the Bing Maps SDK
After you have the location information from your call to GetGeopositionAsync, you’ll want to display it on a map.
- First, convert the Geoposition into a Location that can be used with Bing Maps, use the following code:
1 Location location = <span>new</span> Location(pos.Coordinate.Latitude, pos.Coordinate.Longitude);
1 promise.done(<br /> <span>function</span> (pos) <br /> <span>// Get the coordinates of the current location.</span><br /> <span>var</span> coord = pos.coordinate,<br /> location = <span>new</span> Microsoft.Maps.Location(coord.latitude, coord.longitude);<br /> <br /> ,<br /> <span>function</span> (err) <br /> <span>// Handle the error.</span><br /> );<br />
- Next, display your location using Bing Maps. You can get the Bing Maps SDK here. You’ll also need to get a Bing Maps Key by registering on the Bing Maps Portal and following the instructions for Getting a Bing Maps Key. When you register make sure you look at the usage restrictions for each type of key and choose one that is appropriate for your app.
Add a Bing Maps control to your designer and insert your key into the credentials.
1 <span><</span><span>Page</span><br /> <span>xmlns</span><span>="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><br /> <span>xmlns:x</span><span>="http://schemas.microsoft.com/winfx/2006/xaml"</span><br /> <span>xmlns:local</span><span>="using:SimpleMapping"</span><br /> <span>xmlns:d</span><span>="http://schemas.microsoft.com/expression/blend/2008"</span><br /> <span>xmlns:mc</span><span>="http://schemas.openxmlformats.org/markup-compatibility/2006"</span><br /> <span>xmlns:Maps</span><span>="using:Bing.Maps"</span><br /> <span>x:Class</span><span>="SimpleMapping.MainPage"</span><br /> <span>mc:Ignorable</span><span>="d"</span><span>></span><br /><br /> <span><</span><span>Grid</span> <span>Background</span><span>="StaticResource ApplicationPageBackgroundThemeBrush"</span><span>></span><br /> <span><</span><span>Maps:Map</span> <span>x:Name</span><span>="Map"</span> <span>Margin</span><span>="0,120,0,0"</span> <span>Credentials</span><span>="Insert Your Bing Maps Key Here"</span><span>/></span><br /> <span></</span><span>Grid</span><span>></span><br /><span></</span><span>Page</span><span>></span>
- Now you can use the following code to the page in your app where the map is displayed. This sets the zoom level of the map and shows the location. Learn more about zoom level in How to display your location on a Bing Map:
1 <span>double</span> zoomLevel = 13.0f;<br /><br />Map.SetView(location, zoomLevel);<br />
Then, you’ll need to create a <div> tag to contain the map, setting the id attribute so that you can access it.
1 <span><</span><span>div</span> <span>id</span>=’<span>map-div</span>’<span>></</span><span>div</span><span>></span>
1 <span>// Load the Microsoft.Maps.Map module and then call getMap when the </span><br /><span>// module has loaded.</span><br /><span>function</span> initMap() <br /> Microsoft.Maps.loadModule(<span>'Microsoft.Maps.Map'</span>, <br /> <br /> callback: getMap, <br /> culture: <span>"en"</span>, <br /> homeRegion: <span>"US"</span> <br /> );<br />}<br /><br /><span>// Get the div to contain the map from the interface of the app.</span><br /><span>function</span> getMap() <br /><br /> <span>// Create a new instance of the Map class, passing in the app ID.</span><br /> <span>var</span> map = <span>new</span> Microsoft.Maps.Map(<br /> document.getElementById(mapDivId), <br /> credentials: “Insert your credentials here” );<br /><br /> <span>// Set the initial zoom level and type of map.</span><br /> map.setView(<br /> mapTypeId: Microsoft.Maps.MapTypeId.road,<br /> zoom: 3<br /> );<br />}<br />
- Finally, any app that uses location must respect the user’s permissions settings, which can change at any time. ; The Weather app uses location to get the weather for the current location. ; But at any time, the user may go into the Settings and turn the Locations permissions off. ; Your app always has to handle the possibility that the Location permission has been disabled. ; In this case, the Weather app displays a “Your location cannot be found. Change your Permissions to allow Weather to user your location.” message. See the User experience guidelines in Guidelines for location-aware apps for examples of handling location permissions.
For full details on using geolocation and the Bing Maps SDK, we have a new video, how-to topic , and code sample that leads you through the steps of asynchronously acquiring your location and then using the Bing Maps SDK to show where you are in a visual manner. The video, how-to topic, and sample guide you through enabling the location capability, getting the location asynchronously, setting the map to a given location, and setting the location icon based on the accuracy of the location data. You can also find more info in Bing Maps for Windows Store Apps.
Location can be a powerful and time saving feature to add to your app. You can provide the user with more pertinent information based on where they are. Whether it’s a list of local radio stations, shopping deals, traffic information, or weather information, the control to tailor that information based on location can make your app more useful. And the ability to make that happen automatically, without user interaction, saves the user time and makes them more likely to use your app.
–Ross Heise, Senior Content Developer, Windows Developer Content
Follow this link: