Using Google Maps in C# Xamarin.Android Application

Like any other Android application built in java, Xamarin also provides you the flexibility of using Google Maps in your Xamarin.Android app. You can use the Maps feature in Android, Windows phone and iPhone, as Xamarin is cross platform development IDE but native API implementation is required for these three target platforms.

This means that shared code to use map control will be written in PCL project whereas a call to use Maps API will be native with each platform. In this article, I ‘ll be discussing how you can use Google Maps in your Android application.

There are the following steps involved in using Google Maps in Xamarin.Andorid.

Initialization

Here, I presume that you have created a Xamarin.Forms (Portable class library) project before proceeding.

Platform Specific API Implementation

    1. AccessCourseLocation
    2. AccessFineLocation
    3. AccessLocationExtraCommands
    4. AccessMockLocation
    5. AccessNetworkState
    6. AccessWifiState
    7. AccessInternet
API

Using Map control in C#

Next, create a class in PCL project and don’t forget to add Xamarin.Forms.Maps package in PCL project. Given below is a sample code to use map control in C#. You can use it in your project to use map control.

Remember, if valid API key is not used, map control will just display grey box.

  1. class MapClass: ContentPage {
  2. public MapClass() {
  3. var map = new Map(
  4. MapSpan.FromCenterAndRadius(
  5. new Position(37, -122), Distance.FromMiles(0.3))) {
  6. IsShowingUser = true,
  7. HeightRequest = 100,
  8. WidthRequest = 960,
  9. VerticalOptions = LayoutOptions.FillAndExpand
  10. };
  11. map.MapType = MapType.Street;
  12. var slider = new Slider(1, 18, 1);
  13. slider.ValueChanged += (sender, e) => {
  14. var zoomLevel = e.NewValue; // between 1 and 18
  15. var latlongdegrees = 360 / (Math.Pow(2, zoomLevel));
  16. map.MoveToRegion(new MapSpan(map.VisibleRegion.Center, latlongdegrees, latlongdegrees));
  17. };
  18. var position = new Position(37, -122); // Latitude, Longitude
  19. var pin = new Pin {
  20. Type = PinType.Place,
  21. Position = position,
  22. Label = "custom pin",
  23. Address = "custom detail info"
  24. };
  25. map.Pins.Add(pin);
  26. var stack = new StackLayout {
  27. Children = {
  28. map,
  29. slider
  30. }
  31. };
  32. Content = stack;
  33. }
  34. }
MapType enumeration can have the following possible values

Now, it’s time to test our Maps application. I’ll recommend to test it using a real Android device because testing in emulator will cause an issue. Emulator does not have Google Play services installed on it and Google Maps heavily rely on it. App in emulator will not run without Google Play services. To install Google play services on your emulator, you can get help from this link.

I tested my App2 Android application on Huawei y625. It worked perfectly fine and produced the results shown in the screenshot.

API

I hope that this tutorial has helped you to get your hands dirty with Google Maps in Xamarin.Android applications and if you have got any issues, please post in comments below. I’ll be looking forward to answering them all.

Thank you