icons

Retrieve Coordinates on Android and iOS device (with Xamarin.forms and Xamarin Essentials)

iFour Team - January 22, 2021

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  •  
  •  
  •  
Retrieve Coordinates on Android and iOS device (with Xamarin.forms and Xamarin Essentials)

In this blog, we will be going to acquire our coordinates and retrieves those coordinates on the default map application. We will create one simple application to get and retrieve coordinates. This will be possible by using the Xamarin Essentials API for Geo-Location and Maps. Xamarin Essentials provides cross-platform APIs for mobile applications.

Xamarin Essentials provides a single cross-platform API, that works with any Xamarin.forms, and all three platforms Android, iOS, or UWP. It can be accessed from shared code. To use Xamarin Essential we have to add NuGet Package to our project. For that add Xamarin.Essentials to project. In solution explorer, right-click on the solution >> Select Manage NuGet Packages >> Search Xamarin.Essentials>> Install-Package into All projects: Android, iOS, UWP, and .NET Standard libraries.

After installing Package add a reference to C# class "using Xamarin.Essentials;".Now we are going to create a simple application for that. The prerequisite is IDE Visual Studio 2019.

Here the steps are:

First of all, create a new project for Android and iOS in visual studio. Choose a Mobile app (Xamarin.Forms). Give appropriate name to the project then select Blank template and platform: Android, iOS. Now, write code in App.XAML file and XAML file as well as in code-behind file.

App.XAML file code: Add this code in the app.xaml file. Here, we will apply a style to the controls and later use these styles using Key in our XAML code when we define control.


MainPage (XAML file): Here, we have created three labels and two buttons and applied styles (using a key) defined in App.Xaml file. Those Two buttons are for Location and go to the default map application, and labels are used to get Latitude and Longitude.




MainPage Code behind file: Here, we will apply APIs using Xamarin Essentials. On the clicked event of the GO TO MAP button we will use Maps API and for the GET YOUR LOCATION button we will use GeoLocation API.

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Essentials;

namespace GeoMap {
  
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage {
        public MainPage() 
{
            InitializeComponent() ;
        }

        // GeoLocation API by Xamarin Essentials
      
        private async void GetYourLocation_btnClicked(object sender, EventArgs e ) {
            try
            {
                var YourLocation = await Geolocation.GetLocationAsync
(new GeolocationRequest()
                {
                    DesiredAccuracy = GeolocationAccuracy.Medium,
                    Timeout = TimeSpan.FromSeconds(30)
                }
);

                if (YourLocation = = null)
                    await DisplayAlert("Attention", "GPS is not available", "Ok");
                else
                    YourLocationLatitude.Text = $"{YourLocation.Latitude}" ;
                    YourLocationLongitude.Text = $"{YourLocation.Longitude}" ;
            }
            catch (Exception ex)
            {
                await DisplayAlert( "Ooops!", $"Something went wrong: {ex.Message }", "Ok") ;
            }
        }
      

        
         // Maps API by Xamarin Essentials
       
        private async void GoToMap_btnClicked(object sender, EventArgs e) {
            try
            {
                await Map.OpenAsync(double.Parse(YourLocationLatitude.Text), double.Parse(YourLocationLongitude.Text), new MapLaunchOptions
                {
                    Name = "Your location",
                    NavigationMode = NavigationMode.None
                }
);
            }
            catch (Exception ex1)
            {
                await DisplayAlert("Ooops!", $" Remenber to search your coordinates, first. ", "Ok");
            }
        }
          }
}

Now, add permission to every mobile platform. For Android add the below code at the end of the AssemblyInfo.cs file in the Android platform go to Properties >>AssemblyInfo.cs

Add this assembly:

“[assembly: UsesFeature("android.hardware.location", Required = false)]
[assembly: UsesFeature("android.hardware.location.gps", Required = false)]
[assembly: UsesFeature("android.hardware.location.network", Required = false)] ”

For iOS go to the iOS platform press F7 on Info.plist file and add this line of code:

Blog

Now, add platform Implementation Specification. It is required only for Android. Add this line of code in MainActivity.cs file (inside Android platform)

Xamarin.Essentials.Platform.Init(this, savedInstanceState);

One Stop Solution for Xamarin Mobile App Development ? Your Search ends here.

Now run the application. When the application launch, configure GPS in the emulator. Click on three dots in the emulator then go to Location and write Location name and save point.

Blog

Image: Configure GPS in Android emulator

Blog

Image: MainPage

Blog

Image: Button GO TO MAP clicked

Blog

Image: Button GET YOUR LOCATION clicked

All the Above screenshots are of Android emulators.

 

In this application, we have used Xamarin Essentials to get and retrieve coordinates. We have created a XAML page in which we have used label and button control for getting coordinates and go to the saved location, and applied style using a key to those controls. The styles were defined in the app.Xaml file in . We have defined the style for label and button control. After creating the XAML page we have used GeoLocation and Map's API in the code-behind file of the XAML page, on the button clicked event. At last for both platforms, we have seen how to add permissions and platform implementation Specification. After that, we have also seen how to configure the GPS in the emulator.

Conclusion

In this blog, we have seen how to acquire and retrieve coordinates. We have done this by using Xamarin Essentials. As discussed earlier Xamarin Essentials provides cross-platform APIs for mobile applications. We have also seen how to use them in our project. We have created a single application and see all the steps to get and retrieve Coordinators on the default map application.

Technology Stacks

Technology that meets your business requirements

Planning a cutting-edge technology software solution? Our team can assist you to handle any technology challenge. Our custom software development team has great expertise in most emerging technologies. At iFour, our major concern is towards driving acute flexibility in your custom software development. For the entire software development life-cycle, we implement any type of workflow requested by the client. We also provide a set of distinct flexible engagement models for our clients to select the most optimal solution for their business. We assist our customers to get the required edge that is needed to take their present business to next level by delivering various out-reaching technologies.

Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo

Our Global Presence

Get advanced technology that will help you find the
right answers for your every business need.

Get in touch

Need a Consultation?

Drop us a line! We are here to answer your questions 24/7.