Add Map To Your UWP App

To add maps to your UWP app, you need MapControl and the following three namespaces:

  • Windows.UI.Xaml.Controls.Maps: This namespace contains MapControl itself.

  • Windows.Devices.Geolocation: This namespace allows you to get user location and contains Geofencing API as well.

  • Windows.Services.Maps: This one supports several utility classes which allows to find location by address (Geocoding) and reverse Geocoding (find address by location) etc.

Also MapControl is not in default XAML namespaces, so we need to add a new namespace to XAML file, as shown below.

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

To use map services in Windows 10 application, you need to acquire a token from the Map Dev Center.

Sign in, Advance to My Account -> My Keys and click on create a new key. Select Universal Windows App as Application type from the drop down list.

create app

Without the map service token, your app works fine but you will see a warning message that MapServiceToken is not specified and without the MapServiceToken you cannot publish your app.

Once you have added the maps namespace in your XAML file, add MapControl using the following code.

  1. <Maps:MapControl  
  2.    x:Name="MapControl1"  
  3.    MapServiceToken="Token Value"  
  4.    ZoomLevel="8"  
  5. />  
Finally run your app. A map like the following will be displayed, you can drag or move it around, zoom in or zoom out.

bing map

This is just the first step towards building cool map apps. The next most basic feature of any map is the ability to display info about points of interest by adding images, shapes, pushpins and etc.

Let's see how we can add a pushpin to our map.
  1. private void AddMapIcon()  
  2. {  
  3.     BasicGeoposition location = new BasicGeoposition();  
  4.     location.Latitude = 33.6667;  
  5.     location.Longitude = 73.1667; 
  6.     MapIcon mapIcon;
  7.     mapIcon = new MapIcon();
  8.     if(mapIcon != null)  
  9.     {  
  10.         MapControl1.MapElements.Remove(mapIcon);  
  11.     }  
  12.     mapIcon.Location = new Geopoint(location);  
  13.     mapIcon.Title = "Pakistan";  
  14.     MapControl1.MapElements.Add(mapIcon);  
  15.     MapControl1.Center = new Geopoint(location);  
  16. }  
At first we declared a variable of type BasicGeoposition structure. It provides the basic information to describe a geographic position. It has three fields latitude, longitude and altitude. The altitude field is used only if we are displaying a 3D map.

We'll use MapIcon with default image (you can also customize image by using Image property), to display a pushpin at our desired geographical point. Line 13 creates a geographic point object for the given position and assigns it to Location property of mapIcon.

Line 15 adds the mapIcon to MapControl1 and line 16 centers our map to the specified location.

Run the app.

run

In the next tutorial we'll learn how to geo-code and reverse geo-code.

Read more articles on Universal Windows App:


Similar Articles