In this article, we will discuss how to integrate Google Maps in Xamarin Forms Projects. The steps are, as follows:
In Visual Studio, create a Xamarin Portable Project. Go to New Project -> Cross-Platform -> Blank App (Xamarin.Form.Portable).
Add NuGet package Xamarin.Forms.Maps for each project. You can do this by right clicking on Solution Explorer and clicking Manage NuGet Package for Solution.
In iOS project, double click the AppDelegate.cs file and Add Xamarin.FormsMaps.Init(); in FinishedLauching Function.
In Android project, double click the MainActivity.cs file and Xamarin.FormsMaps.Init(); in Oncreate Function.
In Windows Runtime Project and Universal Windows Platform(UWP) project, double click the MainPage.xaml.cs and add Xamarin.FormsMaps.Init("INSERT_AUTHENTICATION_TOKEN_HERE");. The Token is discussed in further Points.
In Android project, we need to create an API key. To create the API key, go to C:\Users\Admin\AppData\Local\Xamarin\Mono and then copy debug.keystore file.
Now, go to C:\Program Files\Java\jre1.8.0_73\bin and paste the copied debug.keystore file.
Open Command Prompt – go C:\Program Files\Java\jre1.8.0_73\bin and run the command:
keytool -list -v -keystore debug.keystore -alias androiddebugkey -storepass android -keypass android Your SHA1 Key is created. Store the SHA1 key which is further required for creating the API Key.
Now, create project, as shown in the below screen shot, by going to the link as https://console.developers.google.com/ and signing in with your Google account.
Next, it's showing New Project popup window; here, give your Project Name to create a new project.
Then, go for Google APIs and select Google Maps Android API.
Enable Google Android API.
After enabling Google API, go to Google API credentials page.
Then, select configure and select the value to Android in the dropdown. Next, click
What credentials do I need? button.
Windows is showing. Create as API Key to give your project a name and click Add Package name and fingerprint button. Then, give package name as google.maps.key. Now, copy and paste the SHA1 Key here.
Then, click
Create API Key button. It returns APIs Page. It will be showing API key.
Then, in this API, a key is added to the AndroidManifest.XML file of a Xamarin Android application. Add the following code, and give package name as google.maps.key in manifest file.
Now, You'll also need to enable appropriate permissions by right-clicking on the Android project and selecting Properties -> Android Manifest, as shown in the below screen.
In Portable project, add page as “Mapspage.cs” and write the code, as given below.
In App.cs file, change the code, as shown in the screenshot.