#Xamarin.Forms: Using Google Maps instead of Apple Maps

download.jpgIf your user has Google Maps installed on their phone, especially for your iPhone users, they probably want to use that instead of Apple Maps. When your app needs to open Maps, it should open Google Maps in this case.

Here’s how to force the use of Google Maps in Xamarin Forms.

First, install the Xamarin.Essentials NuGet into all your projects first.

When Google Maps is installed, it tells the OS that it’s there and it can be called. To determine if it’s there, we are going to check for it using Xamarin.Essentials:

var supportsUri = await Launcher.CanOpenAsync("comgooglemaps://");

If we find it’s there, then we can call it:

await Launcher.OpenAsync($"comgooglemaps://?center={location.Latitude},{location.Longitude}&zoom=12");

If not then we can just open the default Maps for the OS:

await Xamarin.Essentials.Map.OpenAsync(location, options);

Here’s the full code:

        public static async Task ShowMap(Location locationstring title = null)
        {
            if (location != null)
            {
                var supportsUri = await Launcher.CanOpenAsync("comgooglemaps://");
                if (supportsUri)
                {
                    await Launcher.OpenAsync($"comgooglemaps://?center={location.Latitude},{location.Longitude}&zoom=12");
                }
                else
                {
	                var options = new MapLaunchOptions { Name = title??"Address" };
                   await Xamarin.Essentials.Map.OpenAsync(locationoptions);
                }
            }
        }

 

Google Maps is beloved. Let your users continue to enjoy it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s