In this post, we’ll guide you through the process of disabling these UI elements (zoom controls, compass, and location buttons) in your Android map implementation using a custom map handler. This approach gives you more control over the user experience and map functionality in your mobile app.

Creating a Custom Map Handler

To disable these controls, we need to customize how the map is rendered on Android. This involves creating a custom map handler that intercepts the way the map is displayed and adjusts its settings.

A detailed tutorial for creating custom map handlers can be found in this great guide by Vladislav Antonyuk. We will extend that concept here.

First, we need to implement a MapCallbackHandler that disables specific controls when the map is ready. This is done in the OnMapReady method, which is triggered when the map is fully loaded and ready for interaction.

using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Controls.Compatibility.Maps.Android;

class MapCallbackHandler : Java.Lang.Object, IOnMapReadyCallback
{
    private readonly IMapHandler mapHandler;

    public MapCallbackHandler(IMapHandler mapHandler)
    {
        this.mapHandler = mapHandler;
    }

    public void OnMapReady(GoogleMap googleMap)
    {
        // Update map with any pins or map state changes
        mapHandler.UpdateValue(nameof(IMap.Pins));
        
        // Disable zoom controls
        googleMap.UiSettings.ZoomControlsEnabled = false;
        
        // Disable the "My Location" button
        googleMap.UiSettings.MyLocationButtonEnabled = false;
        
        // Disable the compass
        googleMap.UiSettings.CompassEnabled = false;

        // Additional settings can be adjusted here, such as disabling tilt or gestures.
    }
}

In the OnMapReady method, we access the googleMap.UiSettings property, which contains several settings that control the map’s UI. In our example, we set the following to false:

  • ZoomControlsEnabled: Disables the zoom buttons.
  • MyLocationButtonEnabled: Removes the My Location button that appears when location services are enabled.
  • CompassEnabled: Hides the compass that appears when the user rotates the map.

You can also adjust other settings here, such as disabling tilt gestures or zoom gestures if needed.