Page 81 - MSDN Magazine, April 2017
P. 81
Figure 6 Enabling the Location Capability for the App
helpful for the map control to initialize near the user’s current location. That way, users can quickly get a view of their local sur- roundings, as well as traffic conditions nearby. To do this, you’ll need to enable the Location capability in the Package.appxmanifest file, as shown in Figure 6. In Visual Studio, double-click on the Package.appxmanifest file in the Solution Explorer pane. In the editor, make sure the checkbox next to Location is checked. Save the file. Now, the app has the Location Capability.
In the MainPage.xaml file add the following Button control:
<Button x:Name="btnCurrentLocation" Content="Current Location" Click="btnCurrentLocation_Click" Margin="3"/>
And add the following event handler in the MainPage.xaml.cs file:
private async void btnCurrentLocation_Click(object sender, RoutedEventArgs e) {
var locationAccessStatus = await Geolocator.RequestAccessAsync(); if (locationAccessStatus == GeolocationAccessStatus.Allowed)
{
Geolocator geolocator = new Geolocator();
Geoposition currentPosition = await geolocator.GetGeopositionAsync(); mapControl.Center = new Geopoint(new BasicGeoposition()
{
Latitude = currentPosition.Coordinate.Latitude,
Longitude = currentPosition.Coordinate.Longitude });
mapControl.ZoomLevel = 12;
The btnCurrentLocation_Click method first queries the GeoLoca- tor object for what the access status the app has to it. Upon the first time the app runs on the user’s system, the system will display a dialog box similar to the one shown in Figure 7. If the user clicks on Yes, the code gets the current position from the Geolocator. Afterward, the code centers the map on the latitude and longitude provided. If the Location capability hasn’t been added, the user wouldn’t have even been prompted with the dialog.
Run the app now, click on the Current Location button, click on Yes if prompted. You should see the map focus in on, or very near, your current location. Accuracy will depend on a number of fac- tors. If your device is connected to a GPS receiver, then your accuracy will be very high—likely down to
around 10 meters. If your device lacks a GPS receiver, the Geo- Locator relies on other means to determine location, such as using Wi-Fi. For more information on how Wi-Fi positioning works, refer to the Wikipedia entry at bit.ly/2jWvmWM. If Wi-Fi is unavailable, the GeoLocator will use the client device’s IP address to determine location, where VPN connections and other factors can significantly lower accuracy.
Maps are truly one of the more essential features of mobile devices.
Wrapping Up
Maps are truly one of the more essential features of mobile devices. Many apps could benefit from the addition of a Map control to visualize data or assist users in finding nearby services. In this column, you saw just how easy it is to add a map to your applica- tion and that, in most cases, access to the mapping services comes
} }
at no cost to you, the developer. n
Frank La Vigne is an independent consultant, where he helps customers leverage technology in order to create a better community. He blogs regularly at FranksWorld.com and has a YouTube channel called Frank’s World TV (FranksWorld.TV).
Thanks to the following technical experts for reviewing this article: Rachel Appel
April 2017 67
Figure 7 Asking the User for Permission to Access Precise Location Information msdnmagazine.com