New to Telerik UI for WinUI? Download free 30-day trial

Data Binding

When displaying a map, you might also want to overlay some additional information. As we've previously seen (in the Introduction help topic) we can define this information statically in XAML. However, it would be much more practical to use the VisualizationLayer's data binding capabilities to dynamically load and display map markers.

The VisualizationLayer simulates functionality of the ItemsControl class so in order to display your map data dynamically you just have to treat the VisualizationLayer as a typical ItemsControl. This means you have to use its ItemsSource and ItemTemplate properties. You can also make a use of the ItemTemplateSelector property, in order to implement advanced visualization depending on the data item.

When loading your map data dynamically there are several attached properties that you can use in order to position and display the marker on its proper place:

  • MapLayer.Location: Represents the latitude and the longitude of the map point.

  • BaseZoomLevel: Represents the zoom level, for which the element should have its scale transformation equal to 1.

  • ZoomRange : Represents the range of zoom levels for which the element should be visible.

  • MinScale: Represents minimum scale factor which will be applied to the framework element when it has BaseZoomLevel property set and we zoom out map control.

  • MaxScale: Represents maximum scale factor which will be applied to the framework element when it has BaseZoomLevel property set and we zoom in map control.

In order to provide the needed data to the visualization layer, you have to create a collection of business objects, which represent the desired data.

Let's first define the business class that represent's our objects:

Example 1

public class MapItem : INotifyPropertyChanged 
{ 
    private double baseZoomLevel = double.NaN; 
    private string caption = string.Empty; 
    private Location location = Location.Empty; 
    private ZoomRange zoomRange = ZoomRange.Empty; 
 
    public MapItem( 
        string caption,  
        Location location,  
        double baseZoomLevel,  
        ZoomRange zoomRange) 
    { 
        this.Caption = caption; 
        this.Location = location; 
        this.BaseZoomLevel = baseZoomLevel; 
        this.ZoomRange = zoomRange; 
    } 
 
    public event PropertyChangedEventHandler PropertyChanged; 
 
    public double BaseZoomLevel 
    { 
        get 
        { 
            return this.baseZoomLevel; 
        } 
        set 
        { 
            this.baseZoomLevel = value; 
            this.OnPropertyChanged("BaseZoomLevel"); 
        } 
    } 
 
    public string Caption 
    { 
        get 
        { 
            return this.caption; 
        } 
 
        set 
        { 
            this.caption = value; 
            this.OnPropertyChanged("Caption"); 
        } 
    } 
 
    public Location Location 
    { 
        get 
        { 
            return this.location; 
        } 
 
        set 
        { 
            this.location = value; 
            this.OnPropertyChanged("Location"); 
        } 
    } 
 
    public ZoomRange ZoomRange 
    { 
        get 
        { 
            return this.zoomRange; 
        } 
 
        set 
        { 
            this.zoomRange = value; 
            this.OnPropertyChanged("ZoomRange"); 
        } 
    } 
 
    private void OnPropertyChanged(string propertyName) 
    { 
        if (this.PropertyChanged != null) 
        { 
            this.PropertyChanged( 
                this,  
                new PropertyChangedEventArgs(propertyName)); 
        } 
    } 
} 
The next step is to define how we want to visualize our data items through the ItemTemplate property of the VisualizationLayer and set the desired bindings in it. In the current example, the marker will be represented by an ellipse:

Example 2

<dataVisualization:RadMap x:Name="radMap" 
                ZoomLevel="8" 
                Center="42.6957539183824, 23.3327663758679"> 
    <dataVisualization:RadMap.Provider> 
        <telerikMap:OpenStreetMapProvider /> 
    </dataVisualization:RadMap.Provider> 
    <telerikMap:VisualizationLayer x:Name="visualizationLayer"> 
        <telerikMap:VisualizationLayer.ItemTemplate> 
            <DataTemplate> 
                <Grid telerikMap:MapLayer.Location="{Binding Location}" 
                      telerikMap:MapLayer.BaseZoomLevel="{Binding BaseZoomLevel}" 
                      telerikMap:MapLayer.ZoomRange="{Binding ZoomRange}" 
                      telerikMap:MapLayer.MaxScale="8" 
                      HorizontalAlignment="Center" 
                      VerticalAlignment="Center"> 
                    <Ellipse x:Name="PART_Ellipse" 
                             Width="20" 
                             Height="20" 
                             Stroke="Red" 
                             StrokeThickness="3" 
                             Fill="Transparent"> 
                        <ToolTipService.ToolTip> 
                            <ToolTip Content="{Binding Caption}" /> 
                        </ToolTipService.ToolTip> 
                    </Ellipse> 
                </Grid> 
            </DataTemplate> 
        </telerikMap:VisualizationLayer.ItemTemplate> 
    </telerikMap:VisualizationLayer> 
</dataVisualization:RadMap> 
The last thing to do is to retrieve the data and set it as the ItemsSource the VisualizationLayer:

Example 3

public partial class ExamplePage: Page 
{ 
    public ExamplePage() 
    { 
        InitializeComponent(); 
 
        this.visualizationLayer.ItemsSource = this.GetMapData(); 
    } 
 
    private ObservableCollection<MapItem> GetMapData() 
    { 
        ObservableCollection<MapItem> data = new ObservableCollection<MapItem>(); 
 
        data.Add( 
            new MapItem( 
                "Sofia",  
                new Location(42.6957539183824, 23.3327663758679),  
                8,  
                new ZoomRange(5, 12))); 
 
        data.Add( 
            new MapItem( 
                "Plovdiv",  
                new Location(42.1429369264591, 24.7498095849434),  
                8,  
                new ZoomRange(5, 12))); 
 
        data.Add( 
            new MapItem( 
                "Burgas",  
                new Location(42.5131732087098, 27.4611884843576),  
                8,  
                new ZoomRange(5, 12))); 
 
        data.Add( 
            new MapItem( 
                "Varna",  
                new Location(43.2073941930888, 27.9275176988258),  
                8,  
                new ZoomRange(5, 12))); 
 
        return data; 
    } 
} 
In this article
Not finding the help you need?