This is a migrated thread and some comments may be shown as answers.

How to show a text over mappolygon at runtime

1 Answer 157 Views
Map
This is a migrated thread and some comments may be shown as answers.
Ruhban
Top achievements
Rank 1
Ruhban asked on 12 Oct 2011, 12:09 PM
Hi,
My requirement is to show the text over the map polygon at runtime.
From the xaml we can do this like:
<telerik:MapPolygon Points="56,-100 56,-108 48,-108 48,-100" Fill="Green" Stroke="Red"
                    StrokeThickness="4" CaptionLocation="52,-104">
            <telerik:MapPolygon.CaptionTemplate>
                <DataTemplate>
                    <Grid Background="Yellow">
                        <telerik:MapLayer.HotSpot="0.50.5">
                            <TextBlock Text="My Custom Text" />
                    </Grid>
                </DataTemplate>
            </telerik:MapPolygon.CaptionTemplate>
        </telerik:MapPolygon>

But from the code behind how we can do the same thing.

MapPolygon CreatePolygon(IEnumerable<Location> points, string areaName)
        {
            var polygon = new MapPolygon();
            polygon.Points = this.pins.Points;
            polygon.ToolTip = areaName;
            // also I want to set the area name over the MapPolygon
           
            return polygon;
        }

1 Answer, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 17 Oct 2011, 06:36 AM
Hello Vishal,

You can use the XamlReader.Parse method from the code behind for building the data template for the MapPolygon.CaptionTemplate property.
The sample code is below.
using System;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
using System.Xml.Linq;
using Telerik.Windows.Controls.Map;
 
namespace CaptionFromCodeBehind
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
 
            var collection = new LocationCollection();
            collection.Add(new Location(56, -100));
            collection.Add(new Location(56, -108));
            collection.Add(new Location(48, -108));
            collection.Add(new Location(48, -100));
 
            var polygon = this.CreatePolygon(collection, "Area Name");
            this.informationLayer.Items.Add(polygon);
        }
 
        private MapPolygon CreatePolygon(LocationCollection points, string areaName)
        {
            var polygon = new MapPolygon();
 
            polygon.ToolTip = areaName;
 
            polygon.Points = points;
            polygon.Fill = new SolidColorBrush(Colors.Green);
            polygon.Stroke = new SolidColorBrush(Colors.Red);
            polygon.StrokeThickness = 4;
 
            polygon.CaptionLocation = new Location(52, -104);
 
            // set the caption template
            XElement xTextBlock = new XElement("TextBlock");
            xTextBlock.SetAttributeValue("Text", areaName);
 
            DataTemplate template = XamlReader.Parse(""
                + "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\""
                + " xmlns:telerik=\"http://schemas.telerik.com/2008/xaml/presentation\">"
                + " <Grid Background=\"Yellow\" telerik:MapLayer.HotSpot=\"0.5, 0.5\">"
                + xTextBlock.ToString(SaveOptions.DisableFormatting)
                + " </Grid>"
                + "</DataTemplate>") as DataTemplate;
 
            polygon.CaptionTemplate = template;
 
            return polygon;
        }
    }
}

Greetings,
Andrey Murzov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
Map
Asked by
Ruhban
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Share this question
or