Introduction

In this article I'll show you how to create a simple Xamarin.Forms application that gets your coordinates and retrieves them on your default map application.
This will be possible using the Xamarin Essentials API for Geolocation and Maps.
If you want, you can download the solution from my GitHub Repository.
Of course this is a very basic solution and, If you want, I invite you to improve it.
Prerequisites
IDE: Visual Studio 2019 (All Versions).
The steps given below will lead you to the goal.
Step 1
Create a new blank Xamarin.Forms project for Android and iOS.
Step 2
Write the code for the App.xaml file.
With this step, we'll define the general graphic aspect of the application controllers.
Go to Solution Explorer -> Double Click on App.xaml, delete the auto-generated code and write the following,
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <Application xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:d="http://xamarin.com/schemas/2014/forms/design"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. mc:Ignorable="d"
  7. x:Class="GeoMap_Test.App">
  8. <Application.Resources>
  9. <Style x:Key="CustomLabel" TargetType="Label">
  10. <Setter Property="FontSize" Value="Large"/>
  11. <Setter Property="FontAttributes" Value="Bold"/>
  12. <Setter Property="BackgroundColor" Value="Ivory"/>
  13. <Setter Property="WidthRequest" Value="150"/>
  14. <Setter Property="HeightRequest" Value="80"/>
  15. <Setter Property="HorizontalTextAlignment" Value="Center"/>
  16. <Setter Property="VerticalTextAlignment" Value="Center"/>
  17. </Style>
  18. <Style x:Key="CustomButton" TargetType="Button">
  19. <Setter Property="BackgroundColor" Value="LightGray"/>
  20. <Setter Property="WidthRequest" Value="180"/>
  21. <Setter Property="HeightRequest" Value="60"/>
  22. <Setter Property="CornerRadius" Value="15"/>
  23. </Style>
  24. </Application.Resources>
  25. </Application>
Step 3
Write the code for the MainPage.xaml file.
This step allows us to define the aspect of the MainPage.
Tip:
Please, note that I used the <StackLayout> tag. If you want, you can create a <Grid>.
Go to Solution Explorer -> Double Click on MainPage.xaml, delete the auto-generated code and write the following,
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:d="http://xamarin.com/schemas/2014/forms/design"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. mc:Ignorable="d"
  7. x:Class="GeoMap_Test.MainPage"
  8. Title="Geo Map Application"
  9. BackgroundImageSource="dreamstime_xxl_88693404.jpg">
  10. <ContentPage.Content>
  11. <StackLayout Spacing="100" VerticalOptions="CenterAndExpand">
  12. <StackLayout Padding="10">
  13. <Label Text="Welcome to the GeoMapUnion test."
  14. HorizontalOptions="Center" FontSize="Large" TextColor="Brown"/>
  15. </StackLayout>
  16. <StackLayout HorizontalOptions="Center">
  17. <Button Text="Get your location" Clicked="GetYourLocation_Clicked"
  18. Style="{StaticResource CustomButton}"/>
  19. </StackLayout>
  20. <StackLayout Orientation="Horizontal" HorizontalOptions="Center" Spacing="60">
  21. <Label Text="Latitude" x:Name="YourLocationLatitude" Style="{StaticResource CustomLabel}"/>
  22. <Label Text="Longitude" x:Name="YourLocationLongitude" Style="{StaticResource CustomLabel}"/>
  23. </StackLayout>
  24. <StackLayout HorizontalOptions="Center">
  25. <Button Text="Go To Map" Clicked="GoToMap_Clicked" Style="{StaticResource CustomButton}"/>
  26. </StackLayout>
  27. </StackLayout>
  28. </ContentPage.Content>
  29. </ContentPage>
Step 4
Write the code-behind for the MainPage.xaml.cs file.
Now, we have to define the behavior, the engine, of the application.
Go to Solution Explorer -> Double Click on MainPage.xaml.cs, delete the auto-generated code and write the following,
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Xamarin.Forms;
  8. using Xamarin.Essentials;
  9. namespace GeoMap_Test {
  10. // Learn more about making custom code visible in the Xamarin.Forms previewer
  11. // by visiting https://aka.ms/xamarinforms-previewer
  12. [DesignTimeVisible(false)]
  13. public partial class MainPage : ContentPage {
  14. public MainPage() {
  15. InitializeComponent();
  16. }
  17. /* With this section, we'll implement the
  18. GeoLocation API by Xamarin Essentials*/
  19. #region GeoLocation API
  20. private async void GetYourLocation_Clicked(object sender, EventArgs e) {
  21. try
  22. {
  23. var yourLocation = await Geolocation.GetLocationAsync(new GeolocationRequest()
  24. {
  25. DesiredAccuracy = GeolocationAccuracy.Medium,
  26. Timeout = TimeSpan.FromSeconds(30)
  27. });
  28. if (yourLocation == null)
  29. await DisplayAlert("Attention", "GPS is not available", "Ok");
  30. else
  31. YourLocationLatitude.Text = $"{yourLocation.Latitude}";
  32. YourLocationLongitude.Text = $"{yourLocation.Longitude}";
  33. }
  34. catch (Exception ex)
  35. {
  36. await DisplayAlert("Ooops!", $"Something went wrong: {ex.Message}", "Ok");
  37. }
  38. }
  39. #endregion
  40. /* With this section we'll implement the
  41. Maps API by Xamarin Essentials*/
  42. #region Maps API
  43. private async void GoToMap_Clicked(object sender, EventArgs e) {
  44. try
  45. {
  46. await Map.OpenAsync(double.Parse(YourLocationLatitude.Text), double.Parse(YourLocationLongitude.Text), new MapLaunchOptions
  47. {
  48. Name = "Your location",
  49. NavigationMode = NavigationMode.None
  50. });
  51. }
  52. catch (Exception ex1)
  53. {
  54. await DisplayAlert("Ooops!", $"Remember to search your coordinates, first.", "Ok");
  55. }
  56. }
  57. #endregion
  58. }
  59. }
Step 4
Copy the Backgroud Image.
Now, download the BackgroudImage.zip file (from the top of the article), unzip and copy the file "dreamstime_xxl_88693404.jpg" into the following folders:
Android,
  • \Resources\drawable
  • \Resources\mipmap-anydpi-v26
  • \Resources\mipmap-hdpi
  • \Resources\mipmap-mdpi
  • \Resources\mipmap-xhdpi
  • \Resources\mipmap-xxhdpi
  • \Resources\mipmap-xxxhdpi
iOS,
  • \Resources
Step 5
Add the correct permissions for every mobile platform.
Android:
Go to Solution Explorer -> GeoMap_Test.Android -> Properties. Double click on AssemblyInfo.cs file and add the following code to the end of the "Add some common permissions" block,
  1. [assembly: UsesFeature("android.hardware.location", Required = false)]
  2. [assembly: UsesFeature("android.hardware.location.gps", Required = false)]
  3. [assembly: UsesFeature("android.hardware.location.network", Required = false)]
iOS
Go to Solution Explorer -> GeoMap_Test.iOS. Hit F7 on the Info.plist file and add the following code to the last code block,
  1. <key>NSLocationWhenInUseUsageDescription</key>
  2. <string>Permission for using GPS.</string>
Step 6
Platform Implementation Specifics for both mobile platforms,
Android
Go to Solution Explorer -> GeoMap_Test.Android -> Double click on MainActivity.cs file and add the following code to the OnCreate Method,
  1. Xamarin.Essentials.Platform.Init(this, savedInstanceState);
iOS
No Platform Implementation Specifics.
Step 7 - Configure the GPS on the emulators.
Now, we have to configure the GPS of both the simulators (of course this step is not necessary if you install the app into your smartphone).
Android
Launch the simulator -> Click on the three dots (menu) - > Location -> Write the Location name -> Save the point -> Ok.
iOS
Launch the simulator -> Click on the gear (Settings) -> On Path choose "Apple".
Step 8
Output for both the mobile platforms.
Well, we've finally arrived to the end. The following images show you the output of the application in both the mobile platforms.
Android
iOS

Conclusion

In this tutorial I've shown you how to create a basic Xamarin.Forms application that gets your coordinates and retrieves them on your default map app.
We've used the Xamarin Essentals APIs to reach this goal.
Of course, you can improve it and it's possible to "clean the code", but It's not the purpose of this demo.
The entire solution is available at this GitHub Repository
Feel free to share your feedback with me.
Thank you so much for your attention and interest.