Universal Windows Platform – Location

Location demonstrates how to use the MapControl

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.


Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…


Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.


The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

Once done select from the Menu, Project, then Add New Item…


Step 6

From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project


Step 7

Once in the Code View for Library.cs the following should be entered:

using System;
using System.Threading.Tasks;
using Windows.Devices.Geolocation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

public class Library
    public async Task<Geopoint> Position()
        return (await new Geolocator().GetGeopositionAsync()).Coordinate.Point;

    public UIElement Marker()
        Canvas marker = new Canvas();
        Ellipse outer = new Ellipse()
            Width = 25,
            Height = 25,
            Margin = new Thickness(-12.5, -12.5, 0, 0),
            Fill = new SolidColorBrush(Color.FromArgb(255, 240, 240, 240))
        Ellipse inner = new Ellipse()
            Width = 20,
            Height = 20,
            Margin = new Thickness(-10, -10, 0, 0),
            Fill = new SolidColorBrush(Colors.Black)
        Ellipse core = new Ellipse()
            Width = 10,
            Height = 10,
            Margin = new Thickness(-5, -5, 0, 0),
            Fill = new SolidColorBrush(Colors.White)
        return marker;

In the Library.cs there are using statements to include the necessary functionality. The Position Method is used to get the current position using Geolocator and Marker is a series of Ellipse elements which will make up shape to appear on the MapControl

Step 8

In the Solution Explorer select MainPage.xaml


Step 9

From the Menu choose View and then Designer


Step 10

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<Grid xmlns:map="using:Windows.UI.Xaml.Controls.Maps">
	<map:MapControl Name="Display"/>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Icon="Map" Label="Location" Click="Location_Click"/>

The first block of XAML the main user interface of the Application, this features a Grid with a MapControl to show a Map. The second block of XAML is the CommandBar which contains the Location option to show the current position on the Map.

Step 11

From the Menu choose View and then Code


Step 12

Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:

Library library = new Library();

private async void Location_Click(object sender, RoutedEventArgs e)
	Windows.Devices.Geolocation.Geopoint position = await library.Position();
	DependencyObject marker = library.Marker();
	Windows.UI.Xaml.Controls.Maps.MapControl.SetLocation(marker, position);
	Windows.UI.Xaml.Controls.Maps.MapControl.SetNormalizedAnchorPoint(marker, new Point(0.5, 0.5));
	Display.ZoomLevel = 12;
	Display.Center = position;

Below the MainPage() Method an instance of the Library Class is created. The Location_Click Method handles the obtaining of a Geopoint location, then the Marker is created and added to the MapControl in the relevant location, the Map itself is centred in the MapControl.

Step 13

In the Solution Explorer select Package.appxmanifest


Step 14

From the Menu choose View and then Designer


Step 15

Finally in the Package.appxmanifest select Capabilities and then make sure the Location option is checked


Step 16

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application


Step 17

After the Application has started running if your Local Machine has access to Location you can then select the Location button, it will then ask to let the application access your precise location and if it can will display your current position


Step 18

To Exit the Application select the Close button in the top right of the Application


Creative Commons License

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s