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


Isn't it fun,

to instantly listen to a blog on the go? PLAY !

 
 

Android-and-iOS-device

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.


<application.resources><style targettype="Label" type="text/css" x:key="CustomLbl"><Setter Property = "FontSize" Value = "Large"/>
            <Setter Property = "FontAttributes" Value = "Bold"/>
            <Setter Property = "BackgroundColor" Value = "Ivory"/>
            <Setter Property = "WidthRequest" Value = "150"/>
            <Setter Property = "HeightRequest" Value = "80"/>
            <Setter Property = "HorizontalTextAlignment" Value = "Center"/>
            <Setter Property = "VerticalTextAlignment" Value = "Center"/></style><style targettype="Button" type="text/css" x:key="CustomBtn"><Setter Property = "BackgroundColor" Value = "LightGray"/>
            <Setter Property = "WidthRequest" Value = "180"/>
            <Setter Property = "HeightRequest" Value = "60"/>
            <Setter Property = "CornerRadius" Value = "15"/></style>
</application.resources>

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.


<contentpage.content>
        <stacklayout spacing="100" verticaloptions="CenterAndExpand">
            <stacklayout padding="10">
                <label fontsize="Large" horizontaloptions="Center" text="Welcome to the GeoMapUnion test." textcolor="Brown">
            </label></stacklayout><label fontsize="Large" horizontaloptions="Center" text="Welcome to the GeoMapUnion test." textcolor="Brown">
            <stacklayout horizontaloptions="Center"><button clicked="GetYourLocation_btnClicked" style="{StaticResource CustomBtn}" text="Get your location"></button></stacklayout>
            <stacklayout horizontaloptions="Center" orientation="Horizontal" spacing="60">
                <label style="{StaticResource CustomLbl}" text="Latitude" x:name="YourLocationLatitude">
                <label style="{StaticResource CustomLbl}" text="Longitude" x:name="YourLocationLongitude">
            </label></label></stacklayout><label style="{StaticResource CustomLbl}" text="Latitude" x:name="YourLocationLatitude"><label style="{StaticResource CustomLbl}" text="Longitude" x:name="YourLocationLongitude">
            <stacklayout horizontaloptions="Center"><button clicked="GoToMap_btnClicked" style="{StaticResource CustomBtn}" text="Go To Map"></button></stacklayout>
        </label></label></label></stacklayout><label fontsize="Large" horizontaloptions="Center" text="Welcome to the GeoMapUnion test." textcolor="Brown"><label style="{StaticResource CustomLbl}" text="Latitude" x:name="YourLocationLatitude"><label style="{StaticResource CustomLbl}" text="Longitude" x:name="YourLocationLongitude">
</label></label></label></contentpage.content><label fontsize="Large" horizontaloptions="Center" text="Welcome to the GeoMapUnion test." textcolor="Brown"><label style="{StaticResource CustomLbl}" text="Latitude" x:name="YourLocationLatitude"><label style="{StaticResource CustomLbl}" text="Longitude" x:name="YourLocationLongitude">

</label></label></label>




One Stop Solution for Xamarin Mobile App Development ?

Your Search ends here.


Android-and-iOS-device

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.


<application.resources><style targettype="Label" type="text/css" x:key="CustomLbl"><Setter Property = "FontSize" Value = "Large"/>
            <Setter Property = "FontAttributes" Value = "Bold"/>
            <Setter Property = "BackgroundColor" Value = "Ivory"/>
            <Setter Property = "WidthRequest" Value = "150"/>
            <Setter Property = "HeightRequest" Value = "80"/>
            <Setter Property = "HorizontalTextAlignment" Value = "Center"/>
            <Setter Property = "VerticalTextAlignment" Value = "Center"/></style><style targettype="Button" type="text/css" x:key="CustomBtn"><Setter Property = "BackgroundColor" Value = "LightGray"/>
            <Setter Property = "WidthRequest" Value = "180"/>
            <Setter Property = "HeightRequest" Value = "60"/>
            <Setter Property = "CornerRadius" Value = "15"/></style>
</application.resources>

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.


<contentpage.content>
        <stacklayout spacing="100" verticaloptions="CenterAndExpand">
            <stacklayout padding="10">
                <label fontsize="Large" horizontaloptions="Center" text="Welcome to the GeoMapUnion test." textcolor="Brown">
            </label></stacklayout><label fontsize="Large" horizontaloptions="Center" text="Welcome to the GeoMapUnion test." textcolor="Brown">
            <stacklayout horizontaloptions="Center"><button clicked="GetYourLocation_btnClicked" style="{StaticResource CustomBtn}" text="Get your location"></button></stacklayout>
            <stacklayout horizontaloptions="Center" orientation="Horizontal" spacing="60">
                <label style="{StaticResource CustomLbl}" text="Latitude" x:name="YourLocationLatitude">
                <label style="{StaticResource CustomLbl}" text="Longitude" x:name="YourLocationLongitude">
            </label></label></stacklayout><label style="{StaticResource CustomLbl}" text="Latitude" x:name="YourLocationLatitude"><label style="{StaticResource CustomLbl}" text="Longitude" x:name="YourLocationLongitude">
            <stacklayout horizontaloptions="Center"><button clicked="GoToMap_btnClicked" style="{StaticResource CustomBtn}" text="Go To Map"></button></stacklayout>
        </label></label></label></stacklayout><label fontsize="Large" horizontaloptions="Center" text="Welcome to the GeoMapUnion test." textcolor="Brown"><label style="{StaticResource CustomLbl}" text="Latitude" x:name="YourLocationLatitude"><label style="{StaticResource CustomLbl}" text="Longitude" x:name="YourLocationLongitude">
</label></label></label></contentpage.content><label fontsize="Large" horizontaloptions="Center" text="Welcome to the GeoMapUnion test." textcolor="Brown"><label style="{StaticResource CustomLbl}" text="Latitude" x:name="YourLocationLatitude"><label style="{StaticResource CustomLbl}" text="Longitude" x:name="YourLocationLongitude">

</label></label></label>




One Stop Solution for Xamarin Mobile App Development ?

Your Search ends here.