How to add different style marker pins in the .NET MAUI Maps?
In the Syncfusion® .NET MAUI Maps control, you can add different style marker pins to the MapLayer through the following steps.
Step 1: Initialize the SfMaps control in your layout and add the MapLayer (e.g., 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 your own marker style using the MarkerTemplate property of the MapTileLayer. To add a 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;
}
}
Download the complete sample from GitHub.
Output:
Conclusion:
I hope you enjoyed learning 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. 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®, try our 30-day free trial to check out our .NET MAUI Maps and other .NET MAUI components.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!