Articles in this section
Category / Section

How to group the markers based on zoom level in the .NET MAUI Maps (SfMaps)?

4 mins read

The Syncfusion® .NET MAUI Maps control allows you to group markers based on the zoom level by using the ZoomLevelChanging event of the MapTileLayer in the SfMaps. This helps in displaying individual markers at high zoom levels and combining them into a single group at lower zoom levels.

Step 1: Add the SfMaps control to your layout, initialize the MapTileLayer, and set the OSM tiles provider in the UrlTemplate property of the MapTileLayer as shown in the following code sample.

XAML:

<map:SfMaps>
   <map:SfMaps.Layer>
   	<map:MapTileLayer x:Name="layer"
   			  UrlTemplate="https://tile.openstreetmap.org/{z}/{x}/{y}.png">
   	</map:MapTileLayer>
   </map:SfMaps.Layer>
</map:SfMap>

Step 2: To add a custom marker, add the image to be displayed as a marker to the project’s root folder or subfolder, right-click the added image, select properties, then select the MauiImage option under Build Action.

Adding a custom marker image in Syncfusion® .NET MAUI Maps

Step 3: Add the image content to the layout’s resources in the form of a DataTemplate and set that template to the MarkerTemplate property of the MapTileLayer as shown in the following code sample.

XAML:

<ContentPage.Resources>
   <ResourceDictionary>
       <DataTemplate x:Key="markerTemplate">
           <StackLayout HorizontalOptions="StartAndExpand"
                        VerticalOptions="Center">
               <Image Source="pin.png"
                      HeightRequest="30" 
                      WidthRequest="30" />
           </StackLayout>
       </DataTemplate>
   </ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
   <map:SfMaps>
       <map:SfMaps.Layer>
           <map:MapTileLayer x:Name="layer"
                             UrlTemplate="https://tile.openstreetmap.org/{z}/{x}/{y}.png"
                             MarkerTemplate="{StaticResource markerTemplate}">
           </map:MapTileLayer>
       </map:SfMaps.Layer>
   </map:SfMaps>
</ContentPage.Content> 

Step 4: Add the MapMarker to the MapTileLayer’s MapMarkerCollection as shown in the following code sample.

XAML:

<map:MapTileLayer.Markers>
   <map:MapMarkerCollection x:Name="markers">
   	<map:MapMarker Latitude="20.766387"
                       Longitude="78.750000"/>
   </map:MapMarkerCollection>
</map:MapTileLayer.Markers> 

Step 5: Initialize the MapZoomPanBehavior of the MapTileLayer’s ZoomPanBehavior and set the EnableZooming property to True.

XAML:

<map:MapTileLayer.ZoomPanBehavior>
   <map:MapZoomPanBehavior EnableZooming="True" />
</map:MapTileLayer.ZoomPanBehavior> 

Step 6: In the MapTileLayer’s ZoomLevelChanging event, add and remove the markers from the marker collection to achieve marker grouping based on the zoom level. In the following code sample, a single marker is maintained for Zoom levels 3 and below, and multiple markers are maintained for Zoom levels 4 and beyond.

C#:

private void Layer_ZoomLevelChanging(object sender, ZoomLevelChangingEventArgs e)
{
   if (e.NewZoomLevel <= 3 && markers.Count > 1)
   {
       markers.Clear();
       markers.Add(new MapMarker { Latitude = 20.766387, Longitude = 78.750000 });
   }
   else if (e.NewZoomLevel >= 4 && markers.Count == 1)
   {
       markers.Clear();
       markers.Add(new MapMarker { Latitude = 12.331952, Longitude = 78.002930 });
       markers.Add(new MapMarker { Latitude = 17.678045, Longitude = 78.793945 });
       markers.Add(new MapMarker { Latitude = 18.346705, Longitude = 73.608398 });
       markers.Add(new MapMarker { Latitude = 21.486297, Longitude = 84.418945 });
       markers.Add(new MapMarker { Latitude = 22.139076, Longitude = 72.905273 });
       markers.Add(new MapMarker { Latitude = 26.463197, Longitude = 81.166992 });
       markers.Add(new MapMarker { Latitude = 26.463197, Longitude = 74.487305 });
       markers.Add(new MapMarker { Latitude = 28.952879, Longitude = 77.915039 });
   }
} 

Download the complete sample from GitHub.

Output:

When the zoom level is 1:

Single maker at zoom level 1 in Syncfusion® .NET MAUI Maps

When the zoom level is 4:

Individual markers at zoom level 4 in Syncfusion® .NET MAUI Maps

Conclusion
I hope you enjoyed learning how to group the markers based on the zoom level in the .NET MAUI Maps (SfMaps).
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, 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. 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