Windows 10 Universal Windows Platform – Location

Step 1

Download Visual Studio Community 2015 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2015 or if it has already been downloaded and installed then start the application you may also need to Enable your device for development.

10-home

Step 2

Once Visual Studio Community 2015 has started select File, then New, then Project… from the Menu.

10-file-new-project

Step 3

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

10-new-project

Step 4

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

10-project-add-new-item

Step 5

From the Add New Item window select Visual C# 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

10-add-new-item-library

Step 6

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 };
        outer.Fill = new SolidColorBrush(Color.FromArgb(255, 240, 240, 240));
        outer.Margin = new Thickness(-12.5, -12.5, 0, 0);
        Ellipse inner = new Ellipse() { Width = 20, Height = 20 };
        inner.Fill = new SolidColorBrush(Colors.Black);
        inner.Margin = new Thickness(-10, -10, 0, 0);
        Ellipse core = new Ellipse() { Width = 10, Height = 10 };
        core.Fill = new SolidColorBrush(Colors.White);
        core.Margin = new Thickness(-5, -5, 0, 0);
        marker.Children.Add(outer);
        marker.Children.Add(inner);
        marker.Children.Add(core);
        return marker;
    }
}

It should then appear as such:

10-library-location

Step 7

From the Solution Explorer select MainPage.xaml

10-mainpage

Step 8

Select from the Menu, View then Designer

10-designer

Step 9

The Design View will be displayed along with the XAML View and in this
next to mc:Ignorable=”d” enter the following XAML:

xmlns:Maps="using:Windows.UI.Xaml.Controls.Maps"

Also in the XAML View above <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> enter the following XAML:

<Page.BottomAppBar>
	<AppBar IsOpen="True" IsSticky="True">
		<StackPanel Orientation="Horizontal">
			<AppBarButton Icon="Map" Label="Location" Click="Location_Click"/>
		</StackPanel>
	</AppBar>
</Page.BottomAppBar>

While still in the XAML View below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”>enter the following XAML:

<Maps:MapControl Name="Display"/>

It should appear as such:

10-xaml-location

Step 10

Select from the Menu, View then Code

10-code

Step 11

Once in the Code View below the public MainPage() { … } the following 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();
	Display.Children.Add(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;
}

It should then appear as such:

10-code-location

Step 12

Then from the Solution Explorer select Package.appxmanifest

10-package

Step 13

Select from the Menu, View then Code

10-code

Step 14

The Code View will be displayed for the Package.appxmanifest and in this below <Capability Name=”internetClient” /> enter the following XML:

<DeviceCapability Name="location" />

It should appear as such:

10-package-location

Step 15

That completes the Windows Universal Application so Save the Project then select the Debug and Local Machine option to run the Application

10-localmachine

Step 16

Once the Application has started the following should then appear

10-localmachine-run-location

Step 17

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

10-localmachine-ran-location

Step 18

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Step 19

Another option is to run as a Windows Phone application, select Debug and select Emulator 10.0.1.0 WVGA 4 inch 512MB option to run the Application

10-emulator

Step 20

Once the Emulator has started the Application should then appear

10-emulator-run-location

Step 21

After the Application has started running if the Emulator you can then select the Location button, it will then ask to be able to access your Location and if it can will display your current location

10-emulator-ran-location

Step 22

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Creative Commons License

Advertisements

One thought on “Windows 10 Universal Windows Platform – Location

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 )

Google+ photo

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

Connecting to %s