Map in xamarin form

 

Maps in xamarin forms
Maps in xamarin forms

Please, support my blog by clicking on our sponsors ad!


First Add Nugget in all projects
Xamarin.Forms.Map  

Lets Configure in IOS project to show map in iphone

 In AppDelegate.cs file, Initialize the map below "global::Xamarin.Forms.Forms.Init();" line in "FinishedLaunching" method

 FormsMaps.Init();  

Add following key in info.plist for permission

    <key>NSLocationWhenInUseUsageDescription </key>
    <string>Allow map to access to your location while you use the app.</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Always Allow map to access to your location</string>
    <key>NSLocationUsageDescription</key>
    <string>We would like to show you a map</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>We would like to show you a map</string>  

Now Let's configure for android project.

In mainactivity class add following code:

 ddpublic class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        const int RequestLocationId = 0;

        readonly string[] LocationPermissions =
        {
            Manifest.Permission.AccessCoarseLocation,
            Manifest.Permission.AccessFineLocation
        };

        protected override void OnStart()
        {
            base.OnStart();

            if ((int)Build.VERSION.SdkInt >= 23)
            {
                if (CheckSelfPermission(Manifest.Permission.AccessFineLocation) != Permission.Granted)
                {
                    RequestPermissions(LocationPermissions, RequestLocationId);
                }
                else
                {
                    // Permissions already granted - display a message.
                }
            }
        }

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            FormsMaps.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            if (requestCode == RequestLocationId)
            {
                if ((grantResults.Length == 1) && (grantResults[0] == (int)Permission.Granted))
                {
                    // Permissions granted - display a message.
                }
                else
                {
                    // Permissions denied - display a message.
                }
            }
            else
            {
                Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

                base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
    }  

Add permission in AndroidManifest.xml file in manifest tag

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  

To show map in android app, we required API for map, lets learn how to create API

Go to link: Google Developer Console and login with google account.

Create a new project


Map in xamarin form
Maps in xamarin forms

Enter Project Name select no organization, if you have organization then select organization name from dropdown list  and click on "create button" and click on create button.

maps in xamarin form

Maps in xamarin forms

After creating project, it will redirect to dashboard, then click on "+ ENABLE APIS AND SERVICES"

MAPS IN XAMARIN FORMS
Maps in xamarin forms

Select "Maps SDK for android", then click on "ENABLE" Button

Maps in xamarin forms
Maps in xamarin forms

From left menu select "Credentials" and click on "+ Create Credentials"

Maps in xamarin forms
Maps in xamarin forms



Select API Key

Map in xamarin form
Maps in xamarin forms

It will give you API Key.
map in xamarin form
Maps in xamarin forms

Now copy that Key, we will use in configuration in xamarin
In manifest.xml file, add following code in application tag

 <meta-data android:name="com.google.android.geo.API_KEY" android:value="Paste copied API key here"/>
 <uses-library android:name="org.apache.http.legacy" android:required="false" />  

Now we will add map in main project.

Xaml file add map control

  <map:Map IsShowingUser="True"></map:Map>  

Run your project, you can view map on your device.

Hope this is helpful to you. please share your feedback in comment section, thanks for reading.

Comments

Popular posts from this blog

Push Notifications in .NET MAUI: A Comprehensive Guide

Explore the UI libraries available for .NET MAUI at no cost.

Push Notification using Firebase in xamarin form (Android and IOS)