Back to Devexpress

MapItemsLayerBase.ShapeTitlesPattern Property

windowsforms-devexpress-dot-xtramap-dot-mapitemslayerbase-0090dd08.md

latest7.6 KB
Original Source

MapItemsLayerBase.ShapeTitlesPattern Property

Gets or sets a pattern used to generate shape titles.

Namespace : DevExpress.XtraMap

Assembly : DevExpress.XtraMap.v25.2.dll

NuGet Package : DevExpress.Win.Map

Declaration

csharp
[DefaultValue("{NAME}")]
public string ShapeTitlesPattern { get; set; }
vb
<DefaultValue("{NAME}")>
Public Property ShapeTitlesPattern As String

Property Value

TypeDefaultDescription
String"{NAME}"

A string value.

|

Remarks

Attributes can be used to specify the title pattern of a map shape. To use an attribute in the pattern, specify its value in braces. For instance, to use an attribute with the name Name , set {Name} to the ShapeTitlesPattern property.

Example

This example demonstrates how to change the appearance of a map dot title. To do this, you should specify the MapItemsLayerBase.ShapeTitlesPattern property.

csharp
using DevExpress.Utils;
using DevExpress.XtraMap;
using System;
using System.Globalization;
using System.Windows.Forms;
using System.Xml.Linq;

namespace CustomizeShapeTitle {

    public partial class Form1 : Form {
        const string dataPath = @"..\..\capitals.xml";

        public Form1() {
            InitializeComponent();
            InitializeMap();     
        }

        private void InitializeMap() {
            // Create a Map control and add it to the form.
            MapControl map = new MapControl() { 
                Dock = DockStyle.Fill, CenterPoint = new GeoPoint(47, 2), 
                ZoomLevel = 4, ToolTipController = new ToolTipController()
            };
            this.Controls.Add(map);

            // Create a layer to load image tiles.
            ImageLayer tilesLayer = new ImageLayer() { 
                DataProvider = new OpenStreetMapDataProvider() 
            };
            map.Layers.Add(tilesLayer);

            // Create a layer to display shapes.
            VectorItemsLayer itemsLayer = new VectorItemsLayer() { 
                // Provide data to generate shape items.
                Data = LoadDataFromXML(dataPath), 

                // Enable shape titles.
                ShapeTitlesVisibility = VisibilityMode.Visible,

                // Each shape has a CityName attribute, which stores the capital name.
                // To display this value as a shape title, let's specify the attribute name in braces.
                ShapeTitlesPattern = "{CityName}",

                // To display a Population attribute as a tooltip, 
                // let's specify the ToolTipPattern property as follows.
                ToolTipPattern = "Population: {Population}"
            };
            map.Layers.Add(itemsLayer);
        }

        private MapItemStorage LoadDataFromXML(string filePath) {
            MapItemStorage storage = new MapItemStorage();

            // Load an XML document from the specified file path.
            XDocument document = XDocument.Load(filePath);
            if (document != null) {
                foreach (XElement element in document.Element("Capitals").Elements()) {
                    // Specify shapes attributes by loaded from an XML file values.
                    double latitude = Convert.ToDouble(element.Element("Latitude").Value, 
                        CultureInfo.InvariantCulture
                    );
                    double longitude = Convert.ToDouble(element.Element("Longitude").Value, 
                        CultureInfo.InvariantCulture
                    );
                    string name = element.Element("Name").Value;
                    uint population = Convert.ToUInt32(element.Element("Population").Value);

                    MapDot capital = new MapDot() { 
                        Location = new GeoPoint(latitude, longitude), Size = 20 
                    };
                    capital.Attributes.Add(new MapItemAttribute() 
                        { Name = "CityName", Type = typeof(string), Value = name });
                    capital.Attributes.Add(new MapItemAttribute() 
                        { Name = "Population", Type = typeof(uint), Value = population });
                    storage.Items.Add(capital);
                }
            }
            return storage;
        }

    }
}
vb
Imports DevExpress.Utils
Imports DevExpress.XtraMap
Imports System
Imports System.Globalization
Imports System.Windows.Forms
Imports System.Xml.Linq

Namespace CustomizeShapeTitle

    Partial Public Class Form1
        Inherits Form

        Private Const dataPath As String = "..\..\capitals.xml"

        Public Sub New()
            InitializeComponent()
            InitializeMap()
        End Sub

        Private Sub InitializeMap()
            ' Create a Map control and add it to the form.
            Dim map As New MapControl() With {.Dock = DockStyle.Fill, .CenterPoint = New GeoPoint(47, 2), .ZoomLevel = 4, .ToolTipController = New ToolTipController()}
            Me.Controls.Add(map)

            ' Create a layer to load image tiles.
            Dim tilesLayer As New ImageLayer() With {.DataProvider = New OpenStreetMapDataProvider()}
            map.Layers.Add(tilesLayer)

            ' Create a layer to display shapes.
            Dim itemsLayer As New VectorItemsLayer() With {.Data = LoadDataFromXML(dataPath), .ShapeTitlesVisibility = VisibilityMode.Visible, .ShapeTitlesPattern = "{CityName}", .ToolTipPattern = "Population: {Population}"}
            map.Layers.Add(itemsLayer)
        End Sub

        Private Function LoadDataFromXML(ByVal filePath As String) As MapItemStorage
            Dim storage As New MapItemStorage()

            ' Load an XML document from the specified file path.
            Dim document As XDocument = XDocument.Load(filePath)
            If document IsNot Nothing Then
                For Each element As XElement In document.Element("Capitals").Elements()
                    ' Specify shapes attributes by loaded from an XML file values.
                    Dim latitude As Double = Convert.ToDouble(element.Element("Latitude").Value, CultureInfo.InvariantCulture)
                    Dim longitude As Double = Convert.ToDouble(element.Element("Longitude").Value, CultureInfo.InvariantCulture)

                    Dim name_Renamed As String = element.Element("Name").Value
                    Dim population As UInteger = Convert.ToUInt32(element.Element("Population").Value)

                    Dim capital As New MapDot() With {.Location = New GeoPoint(latitude, longitude), .Size = 20}
                    capital.Attributes.Add(New MapItemAttribute() With {.Name = "CityName", .Type = GetType(String), .Value = name_Renamed})
                    capital.Attributes.Add(New MapItemAttribute() With {.Name = "Population", .Type = GetType(UInteger), .Value = population})
                    storage.Items.Add(capital)
                Next element
            End If
            Return storage
        End Function

    End Class
End Namespace

See Also

MapItemsLayerBase Class

MapItemsLayerBase Members

DevExpress.XtraMap Namespace