Implementing Maps in .NET MAUI


Discover ways to add maps to your .NET MAUI functions in a really quick and straightforward manner, with the IMap interface.

Howdy! Most likely throughout your developer life you’ve gotten had the necessity to add maps to your apps. It is a quite common requirement which you should be ready to face. That’s the reason on this article I will likely be instructing you the way to add maps to your .NET
MAUI
functions in a really quick and straightforward manner, all due to the IMap interface.

Settings per Platform

To ensure that maps are built-in into our app appropriately, we should be sure that to have applicable configurations. That’s why I depart you the set of directions that you must apply for every platform.

  Android

To launch the Maps app, Android makes use of the geo URI scheme, which can enable the consumer to make use of any app already put in on the system that helps this URI scheme.

Go to Platforms ➡ Android ➡ AndroidManifest.xml file and inside Manifest nodes add the next:

<queries>
    <intent>
	    <motion android:title="android.intent.motion.VIEW" />
	    <information android:scheme="geo"/>
    </intent>
</queries>

  Home windows

No setup is required.

  iOS / macOS

No setup is required.

How Do Maps Work in .NET MAUI?

.NET MAUI has the IMap interface accessible, which permits your functions to open your put in map apps with particular information comparable to a placemark or a particular location. It’s contained within the Microsoft.Maui.ApplicationModel namespace and you need to use it by way of the Map.Default property.

How Can I Open a Map?

To open a map, you need to use the OpenAsync methodology. This comprises six overloads. On this article, we are going to outline all of the parameters that settle for its completely different overloads and in the long run you solely have to pick those that adapt to the overload
of your alternative.

Earlier than shifting on to the reason of the parameters, let’s have a look at the accessible overload choices.

Option 1: Location - Option 2: Placemark - Option 3: Latitude, Longitude - Option 4: Location, Option 5: Placemark, MapLaunchOptions ; Option 6: Latitude, Longitude, MapLaunchOptions.

Parameters You Want To Know

Let’s proceed explaining the parameters.

Location: This worth is made up of the latitude and longitude of the handle you need to open in Maps.

var location = new Location(18.481077500388174, -69.918856715392);

Placemark: It provides you extra detailed details about the situation of your map—info such because the title of the nation, thoroughfare, locality, amongst others. Let’s see an instance of the way to do it:

var placemark = new Placemark
{ 
	CountryName = "Dominican Republic",
	AdminArea = "SD",
	Thoroughfare = "Nationwide Theater",
	Locality = "Esperilla",
	CountryCode = "10204"
};

Latitude: It’s a Double worth which receives a latitude as worth.

Instance: 47.645160

Longitude: It’s a Double worth which receives a longitude as worth.

Instance: -122.1306032

MapLaunchOptions: These choices are the set of options that the map can have. Inside these choices to show, we have now the next:

  • Title: It’s the title that will likely be displayed on the Map, based mostly on the coordinates which are added within the Location.
  • NavigationMode: It’s the kind of navigation with which the map will likely be opened. Inside the navigation modes we have now the next:
=”telerik-retableoddrow-2″>

🚴 Bicycling
🚗 Driving
🛣️ Transit
🚶 Strolling

We may have a end result like this:

var choices = new MapLaunchOptions { Title = "Statue of Liberty Museum",
NavigationMode= NavigationMode.Driving };

OpenAsync

Lastly, let’s open the Map with the OpenAsync methodology.

public async Job OpenMap()
{
    var location = new Location(47.645160, -122.1306032);
    var choices = new MapLaunchOptions { Title = "Microsoft Constructing 25" };
    await Map.Default.OpenAsync(location, choices);
}

Make Positive To Verify if the Map Opened

Don’t overlook that opening the map can fail, both as a result of it doesn’t have an utility put in or as a result of it doesn’t have the required permissions. That’s why it’s advisable that you simply at all times examine if the map opened or not.

That is achieved with the TryOpenAsync methodology. It has the identical overloads because the OpenAsync methodology and returns a Bool as a response.

if (await Map.Default.TryOpenAsync(location, choices) == true)
{
    // Write the code for when it opens your map.
}

Platform Limitations

The NavigationMode will likely be offered in your utility if the platform permits it. Beneath I present you the help for every of them:

  Android

  • Bicycling, Driving and Strolling.

  iOS / macOS

  • Driving, Transit and Strolling.

  Home windows

  • Driving, Transit and Strolling.

Wrapping Up

And completed! 💪 You might be prepared to start out implementing Maps in your .NET MAUI functions!! 💚💕

See you subsequent time! 🙋‍♀️

Reference:

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles