Open the map app

Browse sample. Browse the sample

This article describes how you can use the .NET Multi-platform App UI (.NET MAUI) IMap interface. This interface enables an application to open the installed map application to a specific location or place mark.

The default implementation of the IMap interface is available through the Map.Default property. Both the IMap interface and Map class are contained in the Microsoft.Maui.ApplicationModel namespace.

Get started

To access the browser functionality, the following platform-specific setup is required.

Android uses the geo: URI scheme to launch the maps application on the device. This may prompt the user to select from an existing app that supports this URI scheme. Google Maps supports this scheme.

In the Platforms/Android/AndroidManifest.xml file, add the following queries/intent nodes to the manifest node:

<queries>
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="geo"/>
  </intent>
</queries>

Using the map

The map functionality works by calling the IMap.OpenAsync method, and passing either an instance of the Location or Placemark type. The following example opens the installed map app at a specific GPS location:

public async Task NavigateToBuilding25()
{
    var location = new Location(47.645160, -122.1306032);
    var options = new MapLaunchOptions { Name = "Microsoft Building 25" };

    try
    {
        await Map.Default.OpenAsync(location, options);
    }
    catch (Exception ex)
    {
        // No map application available to open
    }
}

Tip

The Location and Placemark types are in the Microsoft.Maui.Devices.Sensors namespace.

When you use a Placemark to open the map, more information is required. The information helps the map app search for the place you're looking for. The following information is required:

public async Task NavigateToBuilding()
{
    var placemark = new Placemark
    {
        CountryName = "United States",
        AdminArea = "WA",
        Thoroughfare = "Microsoft Building 25",
        Locality = "Redmond"
    };
    var options = new MapLaunchOptions { Name = "Microsoft Building 25" };

    try
    {
        await Map.Default.OpenAsync(placemark, options);
    }
    catch (Exception ex)
    {
        // No map application available to open or placemark can not be located
    }
}

Testing if the map opened

There's always the possibility that opening the map app failed, such as when there isn't a map app or your app doesn't have the correct permissions. For each IMap.OpenAsync method overload, there's a corresponding IMap.TryOpenAsync method, which returns a Boolean value indicating that the map app was successfully opened. The following code example uses the TryOpenAsync method to open the map:

var location = new Location(47.645160, -122.1306032);
var options = new MapLaunchOptions { Name = "Microsoft Building 25" };

if (await Map.Default.TryOpenAsync(location, options) == false)
{
    // Map failed to open
}

Extension methods

As long as the Microsoft.Maui.Devices.Sensors namespace is imported, which a new .NET MAUI project automatically does, you can use the built-in extension method OpenMapsAsync to open the map:

public async Task NavigateToBuildingByPlacemark()
{
    var placemark = new Placemark
    {
        CountryName = "United States",
        AdminArea = "WA",
        Thoroughfare = "Microsoft Building 25",
        Locality = "Redmond"
    };

    var options = new MapLaunchOptions { Name = "Microsoft Building 25" };

    try
    {
        await placemark.OpenMapsAsync(options);
    }
    catch (Exception ex)
    {
        // No map application available to open or placemark can not be located
    }
}

Add navigation

When you open the map, you can calculate a route from the device's current location to the specified location. Pass the MapLaunchOptions type to the Map.OpenAsync method, specifying the navigation mode. The following example opens the map app and specifies a driving navigation mode:

public async Task DriveToBuilding25()
{
    var location = new Location(47.645160, -122.1306032);
    var options = new MapLaunchOptions { Name = "Microsoft Building 25",
                                         NavigationMode = NavigationMode.Driving };

    try
    {
        await Map.Default.OpenAsync(location, options);
    }
    catch (Exception ex)
    {
        // No map application available to open
    }
}

Platform differences

This section describes the platform-specific differences with the maps API.

NavigationMode supports Bicycling, Driving, and Walking.