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.
- Windows 10 (Recommended)
- 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.
- using Windows.Devices.Geolocation;
- using Windows.Storage;
- using Windows.Storage.FileProperties;
- using Windows.Storage.Pickers;
- private StorageFile file = null;
- private async void btnSfile_Click(object sender, RoutedEventArgs e) {
- var picker = new FileOpenPicker();
- picker.FileTypeFilter.Add(".jpg");
- picker.FileTypeFilter.Add(".jpeg");
- picker.FileTypeFilter.Add(".mp4");
- picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
- file = await picker.PickSingleFileAsync();
- if (file != null) {
- TblFName.Text = "File Name: " + file.DisplayName;
- }
- }
- private async void GetGeotag(object sender, RoutedEventArgs e) {
- Geopoint geopoint = await GeotagHelper.GetGeotagAsync(file);
- if (geopoint != null) {
- tblLati.Text = "Latitude = " + geopoint.Position.Latitude;
- tblLong.Text = " Longitude = " + geopoint.Position.Longitude;
- } else {
- tblstatus.Text = "No location information available";;
- }
- }
- private async void SetGeotag(object sender, RoutedEventArgs e) {
- GeolocationAccessStatus status = await Geolocator.RequestAccessAsync();
- if (status != GeolocationAccessStatus.Allowed) {
- tblstatus.Text = "Location access is not allowed";
- return;
- }
- Geolocator geolocator = new Geolocator();
- geolocator.DesiredAccuracy = PositionAccuracy.High;
- await GeotagHelper.SetGeotagFromGeolocatorAsync(file, geolocator);
- tblstatus.Text = "Geolocation set to current location";
- }
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.
- <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">
- <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <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" />
- <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" />
- <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" />
- <TextBlock x:Name="TblFName" HorizontalAlignment="Left" Margin="418,164,0,0" Text="" TextWrapping="Wrap" VerticalAlignment="Top" Height="30" Width="205" FontSize="12">File Name :
- <Run x:Name="FileDisplayName" />
- </TextBlock>
- <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" />
- <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" />
- <TextBlock x:Name="tblLong" HorizontalAlignment="Left" Margin="431,260,0,0" Text="Longtitude : " TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" />
- <TextBlock x:Name="tblLati" HorizontalAlignment="Left" Margin="431,306,0,0" Text="Latitude : " TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" Width="96" />
- <TextBlock x:Name="tblstatus" HorizontalAlignment="Left" Margin="232,208,0,0" Text="" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" Width="340" /> </Grid>
- </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.