Setting Or Retrieving Geotag In Media File In UWP With XAML And C#

Geotagging is the process of adding geographical identification data to various media files. This data usually consists of latitude and longitude coordinates etc. Geotagging can tell users the location of the content of a given picture or other media or the point of view, and conversely, some media platforms show the media relevant to a given location.

Reading this article, you can learn how to set or retrieve the Geotagging in Media File in Universal Windows Apps with XAML and Visual C#.

The following important tools are required for developing a UWP application.

  1. Windows 10 (Recommended)
  2. Microsoft Visual Studio 2017

Step 1

Open Visual Studio 2017. Go to Start -> New Project, select Windows Universal (under Visual C#)-> Blank App (Universal Windows). Give a suitable name for your App (UWPGeoTag) and click OK.

 

After choosing the Target and minimum platform version that your Windows Universal Application will support, the Project creates App.xaml and MainPage.xaml.

 

Step 2

Add the following controls for selecting the file and displaying the GeoTag Information. Add the TextBlock and Button Controls and change the Name and Text Property of each.

 

Step 3

Now, add the following namespace and code in the Mainpage.xaml.cs.

  1. using Windows.Devices.Geolocation;  
  2. using Windows.Storage;  
  3. using Windows.Storage.FileProperties;  
  4. using Windows.Storage.Pickers;  
  5. private StorageFile file = null;  
  6. private async void btnSfile_Click(object sender, RoutedEventArgs e) {  
  7.     var picker = new FileOpenPicker();  
  8.     picker.FileTypeFilter.Add(".jpg");  
  9.     picker.FileTypeFilter.Add(".jpeg");  
  10.     picker.FileTypeFilter.Add(".mp4");  
  11.     picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;  
  12.     file = await picker.PickSingleFileAsync();  
  13.     if (file != null) {  
  14.         TblFName.Text = "File Name: " + file.DisplayName;  
  15.     }  
  16. }  
  17. private async void GetGeotag(object sender, RoutedEventArgs e) {  
  18.     Geopoint geopoint = await GeotagHelper.GetGeotagAsync(file);  
  19.     if (geopoint != null) {  
  20.         tblLati.Text = "Latitude = " + geopoint.Position.Latitude;  
  21.         tblLong.Text = " Longitude = " + geopoint.Position.Longitude;  
  22.     } else {  
  23.         tblstatus.Text = "No location information available";;  
  24.     }  
  25. }  
  26. private async void SetGeotag(object sender, RoutedEventArgs e) {  
  27.     GeolocationAccessStatus status = await Geolocator.RequestAccessAsync();  
  28.     if (status != GeolocationAccessStatus.Allowed) {  
  29.         tblstatus.Text = "Location access is not allowed";  
  30.         return;  
  31.     }  
  32.     Geolocator geolocator = new Geolocator();  
  33.     geolocator.DesiredAccuracy = PositionAccuracy.High;  
  34.     await GeotagHelper.SetGeotagFromGeolocatorAsync(file, geolocator);  
  35.     tblstatus.Text = "Geolocation set to current location";  
  36. }  

Step 4

Go to Project Menu,  select UWPGeoTag Properties -> Package.appmanifest -> Capabilities and enable the "Location" option.

 

Note

The following code will be generated automatically in XAML code View while we are done in the design view.

  1. <Page x:Class="UWPGeoTag.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPGeoTag" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">  
  2.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  3.         <TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="19,18,0,0" Text="Setting and Retrieving In GeoTag to Media file" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="36" FontWeight="Bold" />  
  4.         <Button x:Name="btnSfile" Content="Select File" HorizontalAlignment="Left" Margin="175,156,0,0" VerticalAlignment="Top" Width="137" FontSize="24" FontWeight="Bold" Click="btnSfile_Click" />  
  5.         <TextBlock x:Name="tblsel" HorizontalAlignment="Left" Margin="75,112,0,0" Text="Please Select the file for set or retrieve Geo Tag" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="24" FontWeight="Normal" />  
  6.         <TextBlock x:Name="TblFName" HorizontalAlignment="Left" Margin="418,164,0,0" Text="" TextWrapping="Wrap" VerticalAlignment="Top" Height="30" Width="205" FontSize="12">File Name :  
  7.             <Run x:Name="FileDisplayName" />  
  8.         </TextBlock>  
  9.         <Button x:Name="btngetgeo" Click="GetGeotag" Content="Get geotag from file" HorizontalAlignment="Left" Margin="165,257,0,0" VerticalAlignment="Top" Height="46" Width="209" FontSize="20" />  
  10.         <Button x:Name="btnsetgeo" Click="SetGeotag" Content="Set geotag To file" HorizontalAlignment="Left" Margin="166,326,0,0" VerticalAlignment="Top" Height="46" Width="209" FontSize="20" />  
  11.         <TextBlock x:Name="tblLong" HorizontalAlignment="Left" Margin="431,260,0,0" Text="Longtitude : " TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" />  
  12.         <TextBlock x:Name="tblLati" HorizontalAlignment="Left" Margin="431,306,0,0" Text="Latitude : " TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" Width="96" />  
  13.         <TextBlock x:Name="tblstatus" HorizontalAlignment="Left" Margin="232,208,0,0" Text="" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" Width="340" /> </Grid>  
  14. </Page>  

Step 7

Deploy your app on the Local Machine. The output of the UWPGeoTag app is shown below.

 

After clicking the "Select File" button -

 

After clicking the "Get geotag from file" button.


After clicking the "Set geotag to file" button.

 
 
After clicking the "Get geotag from file" button. (Now, geotag is set to the current location).

Summary

We have successfully tested setting and retrieving a Geotag in a media file in Visual C# and UWP environment using Visual Studio 2017.

Up Next
    Ebook Download
    View all
    Learn
    View all