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. <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.googlemap.googlemap" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
  3. <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="23" />
  4. <application android:label="googlemap">
  5. <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="YOUR_API KEY" />
  6. <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
  7. </application>
  8. <user-permission android:name="com.googlemap.googlemap.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
  9. <uses-permission android:name="com.googlemap.googlemap.permission.MAPS_RECEIVE" />
  10. <uses-permission android:name="com.google.android.providers.gsf.permisson.READ_GSERVICES" />
  11. <uses-permission android:name="android.permission.INTERNET" />
  12. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  13. </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. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <fragment
  7. android:id="@+id/googlemap"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. class="com.google.android.gms.maps.MapFragment" />
  11. </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: