Articles in this section
Category / Section

How to add different style marker pins in the .NET MAUI Maps?

5 mins read

In the Syncfusion .NET MAUI Maps control, you can add different style marker pins to the MapTileLayer, through the following steps.

Step 1: Initialize the SfMaps control in your layout and add the MapTileLayer to the SfMaps‘s Layer, as shown in the following code sample.

XAML:

<map:SfMaps>
   <map:SfMaps.Layer>
   	<map:MapTileLayer x:Name="layer"/>
   </map:SfMaps.Layer>
</map:SfMaps> 

Step 2: Create a custom marker by extending the MapMarker with required additional properties and add the custom markers with Latitude, Longitude, and Population (in million) values to the MapTileLayer using the Markers property as shown in the following code.

XAML:

<map:MapTileLayer x:Name="layer"
                 UrlTemplate="https://tile.openstreetmap.org/{z}/{x}/{y}.png">
   <map:MapTileLayer.Markers>
       <map:MapMarkerCollection>
           <local:CustomMarker Population="321"
                               Latitude="37.090240"
                               Longitude="-95.712891" />
           <local:CustomMarker Population="204"
                               Latitude="-4.038333"
                               Longitude="21.758663" />
           <local:CustomMarker Population="137"
                               Latitude="48.019573"
                               Longitude="66.923683" />
           <local:CustomMarker Population="255"
                               Latitude="-25.274399"
                               Longitude="133.775131" />
       </map:MapMarkerCollection>
   </map:MapTileLayer.Markers>
</map:MapTileLayer> 

C#:

public class CustomMarker : MapMarker
{
   /// <summary>
   /// Gets or sets marker image for high population.
   /// </summary>
   public ImageSource HighPopulationMarker { get; set; }

   /// <summary>
   /// Gets or sets marker image for low population.
   /// </summary>
   public ImageSource LowPopulationMarker { get; set; }

   /// <summary>
   /// Gets or sets population (in million).
   /// </summary>
   public int Population { get; set; }

   public CustomMarker()
   {
       HighPopulationMarker = ImageSource.FromResource("StyleMarkerPins.PopulationMarkerPin1.png");
       LowPopulationMarker = ImageSource.FromResource("StyleMarkerPins.PopulationMarkerPin2.png");
   }
}

Step 3: Define the own marker style using the MarkerTemplate property of the MapTileLayer. To add the different style marker based on the marker data, change the MarkerTemplate using the DataTemplateSelector as shown in the following code.

XAML:

<ContentPage.Resources>
   <ResourceDictionary>
   	<DataTemplate x:Key="HighPopulationTemplateKey">
   		<StackLayout IsClippedToBounds="false"
   		             HorizontalOptions="StartAndExpand"
   		             VerticalOptions="Center"
   		             HeightRequest="30">
   			<Image Source="{Binding  HighPopulationMarker}"
   			       Scale="1"
   			       Aspect="AspectFit"
   			       HorizontalOptions="StartAndExpand"
   			       VerticalOptions="Center"
   			       HeightRequest="15"
   			       WidthRequest="23"/>
   		</StackLayout>
   	</DataTemplate>
   	<DataTemplate x:Key="LowPopulationTemplateKey">
   		<StackLayout IsClippedToBounds="false"
   		             HorizontalOptions="StartAndExpand"
   		             VerticalOptions="Center"
   		             HeightRequest="30">
   			<Image Source="{Binding  LowPopulationMarker}"
   			       Scale="1"
   			       Aspect="AspectFit "
   			       HorizontalOptions="StartAndExpand"
   			       VerticalOptions="Center"
   			       HeightRequest="15"
   			       WidthRequest="23" />
   		</StackLayout>
   	</DataTemplate>
   	<local:MapsDataTemplateSelector x:Key="MapsDataTemplateSelectorKey"
   		                        HighPopulationTemplate="{StaticResource HighPopulationTemplateKey}"
   		                        LowPopulationTemplate="{StaticResource LowPopulationTemplateKey}" />
   </ResourceDictionary>
</ContentPage.Resources>
...
<map:SfMaps.Layer>
   <map:MapTileLayer MarkerTemplate="{StaticResource MapsDataTemplateSelectorKey}">
   	...
   </map:MapTileLayer>
</map:SfMaps.Layer>
... 

C#:

public class MapsDataTemplateSelector : DataTemplateSelector
{
   public DataTemplate HighPopulationTemplate { get; set; }

   public DataTemplate LowPopulationTemplate { get; set; }

   protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
   {
       return ((CustomMarker)item).Population >= 250 ? HighPopulationTemplate : LowPopulationTemplate;
   }
}

Output:

Different-style-marker-pins.png

Conclusion:

Hope you enjoyed learning about how to add different style marker pins in the .NET MAUI Maps.

You can refer to our .NET MAUI Maps feature tour page to learn about its other groundbreaking feature representations. You can explore our .NET MAUI Maps documentation to understand how to present and manipulate data.

For current customers, you can check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our .NET MAUI Maps and other .NET MAUI components.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied