Xamarin.Essentials: Map

The Map class enables an application to open the installed map application to a specific location or placemark.

Get started

To start using this API, read the getting started guide for Xamarin.Essentials to ensure the library is properly installed and set up in your projects.

Using Map

Add a reference to Xamarin.Essentials in your class:

using Xamarin.Essentials;

The Map functionality works by calling the OpenAsync method with the Location or Placemark to open with optional MapLaunchOptions.

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

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

When opening with a Placemark, the following information is required:

  • CountryName
  • AdminArea
  • Thoroughfare
  • Locality
public class MapTest
{
    public async Task NavigateToBuilding25()
    {
        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.OpenAsync(placemark, options);
        }
        catch (Exception ex)
        {
            // No map application available to open or placemark can not be located
        }
    }
}

Extension Methods

If you already have a reference to a Location or Placemark, you can use the built-in extension method OpenMapAsync with optional MapLaunchOptions:

public class MapTest
{
    public async Task OpenPlacemarkOnMap(Placemark placemark)
    {
        try
        {
            await placemark.OpenMapAsync();
        }
        catch (Exception ex)
        {
            // No map application available to open
        }
    }
}

Directions Mode

If you call OpenMapAsync without any MapLaunchOptions, the map will launch to the location specified. Optionally, you can have a navigation route calculated from the device's current position. This is accomplished by setting the NavigationMode on the MapLaunchOptions:

public class MapTest
{
    public async Task NavigateToBuilding25()
    {
        var location = new Location(47.645160, -122.1306032);
        var options =  new MapLaunchOptions { NavigationMode = NavigationMode.Driving };

        await Map.OpenAsync(location, options);
    }
}

Platform Differences

  • NavigationMode supports Bicycling, Driving, and Walking.

Platform Implementation Specifics

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. Xamarin.Essentials is tested with Google Maps, which supports this scheme.

API

Find more Xamarin videos on Channel 9 and YouTube.