Simple Custom Pin Code Sample

Developer
Apr 10, 2009 at 1:07 AM
Add a Style to your Silverlight application for your custom pin.

in my App.xaml file

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:SilverlightSurf"
             x:Class="SilverlightSurf.App">
 
    <Application.Resources>
 
        <Style x:Name="MyPointStyle" TargetType="local:MyPoint">
            <Setter Property="RenderTransformOrigin" Value="0.5,0.5" />
            <Setter Property="AnchorPoint" Value="0.5,1.0" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:MyPoint">
                        <Grid x:Name="_LayoutRoot">
                            <Image x:Name="_Image" Stretch="UniformToFill" Height="32" Width="32" Source="Images/bts.png"/>
                            <TextBlock x:Name="_TextBlock"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
 
    </Application.Resources>
 
</Application>

A class for me custom pin MyPoint.cs, inherits from PontBase class.

using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using DeepEarth.Geometry;
 
namespace SilverlightSurf
{
 
    public class MyPoint : PointBase
    {
        public string LableText { get; set; }
        private TextBlock _TextBlock;
 
        public MyPoint()
        {
            Style = Application.Current.Resources["MyPointStyle"] as Style;
        }
 
        public override void OnApplyTemplate()
        {
            //base.OnApplyTemplate();
 
            _TextBlock = (TextBlock)GetTemplateChild("_TextBlock");
            _TextBlock.Text = LableText;
 
            if(HtmlPage.IsEnabled)
            {
                _IsLoaded = true;
                Layer.UpdateChildLocation(this);
            }
        }
    }
}

In my Page.xaml.cs file to create and add the Custom pin to my map

using System.Windows;
using DeepEarth.Geometry;
using DeepEarth.Provider.OpenStreetMaps;
 
namespace SilverlightSurf
{
    public partial class Page
    {
        public Page()
        {
            InitializeComponent();
            map.BaseLayer.Source = new OsmTileSource(OsmMapModes.Mapnik);
 
            var geoLayer = new GeometryLayer(map) {UpdateMode = GeometryLayer.UpdateModes.ElementUpdate};
            map.Layers.Add(geoLayer);
 
            var myPoint = new MyPoint {LableText = "this is a text", Point = new Point(0, 0)};
            geoLayer.Add(myPoint);
        }
    }
}


Jun 4, 2009 at 1:54 PM
Thats cool, but what if I wanted a custom that was an actual control with probably animations or so on, when I try applying style with those features, the program crashes, would I have to add it the canvas rather than the geolayer or what?
Jun 22, 2009 at 8:34 AM

Hello,

I'm trying to apply the same approach for a Polygon control instead of a Point, but I can't seem to get the TextBlock to show. As a matter of fact I can't get anything but the Path of the polygon to show.

What am I doing wrong?

Thx

Jun 23, 2009 at 1:33 PM

OK, I started over and managed to get the TextBlocks to show, but now the TextBlocks all appear on the top left corner of the map. I tried setting their position using GeometryLayer.SetLeft / SetTop, and although it does place them on different locations over the map, they do not update their position when panning or zooming.

I guess the UpdateChildLocation method of GeometryLayer should be applied somehow to whatever controls added to geometries.

What would be the right approach to do that? 

Jul 6, 2009 at 7:13 PM

Hello

I added a piechart to my map as described above. My problem is that the chart is anchored at the upper left corner. How can I do it, that the chart will be placed in the middle of it?

Thanks for help,

Stefan.