Xamarin Android: Create Xamarin Android Google Map With Add Markers

In this article you will create a Xamarin Android Google map with Marker.

Let’s start

Before we create a Google Map application we need  Signed Fingerprint SHA1 KEY. So, I already explained how to create SHA1 KEY.

Step 1:

Open Visual Studio, New Project, Templates, Visual C#, Android, then click Blank App, and s
elect Blank App. Then give Project Name and Project Location. 

 
Step 2:Next Click to select Solution Explorer->Project Name->Properties-> AndroidManifest.xml file.
 
 
Step 3: Here will be Copy and paste of the already created Google Map SHA1 KEY and alsoit will give some permissions.
 

XML Code:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.googlemap.googlemap" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">  
  4. <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="23" />  
  5.   
  6. <application android:label="googlemap">  
  7.   
  8. <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="YOUR_API KEY" />  
  9.   
  10. <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />  
  11.   
  12. </application>  
  13.   
  14. <user-permission android:name="com.googlemap.googlemap.permission.MAPS_RECEIVE" android:protectionLevel="signature" />  
  15.   
  16. <uses-permission android:name="com.googlemap.googlemap.permission.MAPS_RECEIVE" />  
  17. <uses-permission android:name="com.google.android.providers.gsf.permisson.READ_GSERVICES" />  
  18.   
  19. <uses-permission android:name="android.permission.INTERNET" />  
  20.   
  21. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  22.   
  23. </manifest>  

Step 4: Next we add Google Map References to click Solution Explorer-> Project Name->Components.
 
 

Step 5:

Then Add packages Google Play services - Maps, then check Solution Explorer-Project Name->References to be added Google play services packages.

Step 6:

Next create Map function. In select Solution Explorer->Project Name->Resources->Layout->Main.axml, Click and open AXML Design Page to added below Code.

 

XML CODE:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5. android:orientation="vertical"  
  6.   
  7. android:layout_width="fill_parent"  
  8.   
  9. android:layout_height="fill_parent">  
  10.   
  11. <fragment  
  12.   
  13.    android:id="@+id/googlemap"  
  14.   
  15.    android:layout_width="match_parent"  
  16.   
  17.    android:layout_height="match_parent"  
  18.   
  19.    class="com.google.android.gms.maps.MapFragment" />  
  20.   
  21. </LinearLayout>  

Step 7: Next open MainActivity.cs page, then Add Namespace and following code.

 

Namespace: using Android.Gms.Maps;

C# Code:

  1. public class MainActivity: Activity, IOnMapReadyCallback  
  2. {  
  3.     private GoogleMap GMap;  
  4.     protected override void OnCreate(Bundle bundle)   
  5.     {  
  6.         base.OnCreate(bundle);  
  7.         // Set our view from the "main" layout resource  
  8.         SetContentView(Resource.Layout.Main);  
  9.         SetUpMap();  
  10.     }  
  11.     private void SetUpMap() {  
  12.         if (GMap == null) {  
  13.             FragmentManager.FindFragmentById < MapFragment > (Resource.Id.googlemap).GetMapAsync(this);  
  14.         }  
  15.     }  
  16.     public void OnMapReady(GoogleMap googleMap) {  
  17.         this.GMap = googleMap;  
  18.     }  
  19. }  

Step 8: Then Debug and run the application then check emulator showing Google map,

 

Step 9:
Next Google map Marker will be added.

Step 10: Open MainActivity.cs page select section of OnMapReady Function to added following code.

 

C# Code:

  1. LatLng latlng = new LatLng(Convert.ToDouble(13.0291), Convert.ToDouble(80.2083));  
  2. CameraUpdate camera = CameraUpdateFactory.NewLatLngZoom(latlng, 15);  
  3. GMap.MoveCamera(camera);  
  4. MarkerOptions options = new MarkerOptions().SetPosition(latlng).SetTitle("Chennai");  
  5. GMap.AddMarker(options);  

Step 11: Then Run the Application,

 

Finally, we have successfully created Xamarin Android Google Map with marker Application.

Read more articles on Xamarin: