Skip to main content

Drawing Elements on Maps with .NET MAUI

Did you know that .NET MAUI features a full Map control for iOS, Android, and MacCatalyst apps? It came with the launch of .NET 7 and it allows you to not only display a map with pins, but it also allows for full drawing with polygons, polylines, and circles! Lets take a look today at the Map control, how to display pins, and how to draw outlines and walking paths on the Microsoft campus in Redmond!

Multiple maps showing different capabilities in .NET MAUI

Pins on a Map

The Map controls in .NET MAUI offers a wide arrange of customization of how to display the map. One of the basic elements that people place on a map is a Pin that displays a marker with text. The Pins property on the Map control is an IList that we can populate dynamically to put as many pins on the map. This can be accomplished in XAML:

<ContentPage ...
             xmlns:maps="clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps"
             xmlns:sensors="clr-namespace:Microsoft.Maui.Devices.Sensors;assembly=Microsoft.Maui.Essentials">
    <maps:Map x:Name="map">
        <x:Arguments>
            <MapSpan>
                <x:Arguments>
                    ...
                </x:Arguments>
            </MapSpan>
        </x:Arguments>
        <maps:Map.Pins>
            <maps:Pin Label="Santa Cruz"
                      Address="The city with a boardwalk"
                      Type="Place">
                <maps:Pin.Location>
                    <sensors:Location>
                        <x:Arguments>
                            <x:Double>36.9628066</x:Double>
                            <x:Double>-122.0194722</x:Double>
                        </x:Arguments>
                    </sensors:Location>
                </maps:Pin.Location>
            </maps:Pin>
        </maps:Map.Pins>
    </maps:Map>
</ContentPage>

You can specify the pins directly in the XAML or you can use data binding to a list of Pin using the ItemsSource property similar to a ListView or CollectionView.

You can also do this directly in C# code:

using Microsoft.Maui.Controls.Maps;
using Microsoft.Maui.Maps;

//...

var pin = new Pin
{
  Label = "Santa Cruz",
  Address = "The city with a boardwalk",
  Type = PinType.Place,
  Location = new Location(36.9628066, -122.0194722)
};

map.Pins.Add(pin);

A map with a pin on Santa Cruz open

Better yet, you can add events to the Pin to see when it was clicked or when the info window was clicked with the MarkerClicked and InfoWindowClicked events on the Pin.

Drawing Elements on a Map

Now, let’s get into some fun and actually draw some lines and polygons on a map! The MapElements property on a Map takes in a list of MapElement that can be drawn on the map. Built into .NET MAUI are three elements:

  • Circle: A circle drawn from a center point with a specified radius
  • Polygon: A fully enclosed shape. The first & last points are automatically connected.
  • Polyline: A series of points connected by drawn lines

With these three elements you can do just about anything on a map! Like drawing an overlay over the Microsoft Campus in Redmond with a line showing the walkway between the East and West campus:

A map showing two regions and a line

First, let’s draw two Polygon elements that represent our West and East campus. Each of them will take in a list of Location on the Geopath property that will define the outline points. The Polygon will automatically connect each of these points together with a Stroke that you can define and a full background FillColor. All of this can be created directly in XAML with our without data binding or directly in C# like I prefer when dealing with maps.

var msWest = new Polygon
{
    StrokeColor = Color.FromArgb("#FF9900"),
    StrokeWidth = 8,
    FillColor = Color.FromArgb("#88FF9900"),
    Geopath =
    {
        new Location(47.6458676, -122.1356007),
        new Location(47.6458097, -122.142789),
        new Location(47.6367593, -122.1428104),
        new Location(47.6368027, -122.1398707),
        new Location(47.6380172, -122.1376177),
        new Location(47.640663, -122.1352359),
        new Location(47.6426148, -122.1347209),
        new Location(47.6458676, -122.1356007)
    }
};

var msEast = new Polygon
{
    StrokeColor = Color.FromArgb("#1BA1E2"),
    StrokeWidth = 8,
    FillColor = Color.FromArgb("#881BA1E2"),
    Geopath =
    {
        new Location(47.6368678, -122.137305),
        new Location(47.6368894, -122.134655),
        new Location(47.6359424, -122.134655),
        new Location(47.6359496, -122.1325521),
        new Location(47.6424124, -122.1325199),
        new Location(47.642463, -122.1338932),
        new Location(47.6406414, -122.1344833),
        new Location(47.6384943, -122.1361248),
        new Location(47.6372943, -122.1376912),
        new Location(47.6368678, -122.137305),
    }
};

With our two Polygon regions defined now, all we need to do is create a Polyline to represent the bridge that connects the two parts of the campus. A Polyline is extremely similar to a Polygon, but it does not automatically “connect” the start and end points, and there is no background fill. It is simply points on a map that are connected by a line.

var interstateBridge = new Polyline
{
    StrokeColor = Colors.Black,
    StrokeWidth = 12,
    Geopath =
    {
        new Location(47.6381401, -122.1317367),
        new Location(47.6381473, -122.1350841),
        new Location(47.6382847, -122.1353094),
        new Location(47.6384582, -122.1354703),
        new Location(47.6401136, -122.1360819),
        new Location(47.6403883, -122.1364681),
        new Location(47.6407426, -122.1377019),
        new Location(47.6412558, -122.1404056),
        new Location(47.6414148, -122.1418647),
        new Location(47.6414654, -122.1432702)
    }
};

Now it is time to load these elements into our Map control’s MapElements property and of course zoom the map to the position above the campus:

map.MapElements.Add(msWest);
map.MapElements.Add(msEast);
map.MapElements.Add(interstateBridge);

map.MoveToRegion(MapSpan.FromCenterAndRadius(
                new Location(47.640663, -122.1376177), Distance.FromMiles(1)));

There you have it! Our map is now fully displaying our two regions and a line that represent the bridge walkway between the campus! You can always add and remove as many MapElements as you would like!

Learn More

There is so much more to learn about the Map control! I recently did a short overview video that I highly recommend watching:

Additionally, read through the entire documention where you will find the full source code for this sample and so much more!

The post Drawing Elements on Maps with .NET MAUI appeared first on .NET Blog.



source https://devblogs.microsoft.com/dotnet/drawing-on-maps-with-dotnet-maui/

Comments

Popular posts from this blog

Fake CVR Generator Denmark

What Is Danish CVR The Central Business Register (CVR) is the central register of the state with information on all Danish companies. Since 1999, the Central Business Register has been the authoritative register for current and historical basic data on all registered companies in Denmark. Data comes from the companies' own registrations on Virk Report. There is also information on associations and public authorities in the CVR. As of 2018, CVR also contains information on Greenlandic companies, associations and authorities. In CVR at Virk you can do single lookups, filtered searches, create extracts and subscriptions, and retrieve a wide range of company documents and transcripts. Generate Danish CVR For Test (Fake) Click the button below to generate the valid CVR number for Denmark. You can click multiple times to generate several numbers. These numbers can be used to Test your sofware application that uses CVR, or Testing CVR APIs that Danish Govt provide. Generate

How To Iterate Dictionary Object

Dictionary is a object that can store values in Key-Value pair. its just like a list, the only difference is: List can be iterate using index(0-n) but not the Dictionary . Generally when we try to iterate the dictionary we get below error: " Collection was modified; enumeration operation may not execute. " So How to parse a dictionary and modify its values?? To iterate dictionary we must loop through it's keys or key - value pair. Using keys

How To Append Data to HTML5 localStorage or sessionStorage?

The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time untill unless user deletes his cache, data stored in sessionStorage gets cleared when the originating window or tab get closed. These are new HTML5 objects and provide these methods to deal with it: The following snippet accesses the current domain's local Storage object and adds a data item to it using Storage.setItem() . localStorage.setItem('myFav', 'Taylor Swift'); or you can use the keyname directly as : localStorage.myFav = 'Taylor Swift'; To grab the value set in localStorage or sessionStorage, we can use localStorage.getItem("myFav"); or localStorage.myFav There's no append function for localStorage or sessionStorage objects. It's not hard to write one though.The simplest solution goes here: But we can kee