ArcGIS API for Silverlight代码中使用Template模板

       在项目开发中,会遇到点选中聚焦闪烁效果,但是因为在使用Symbol的时候,会设置一定的OffSetX和OffSetY,所以聚焦闪烁的时候,有些情况下,会出现闪烁点的位置和Symbol的位置不重叠现象,下面的方法就是解决这个问题的。

1、在Silverlight项目中新建一个文件夹Template,新建一个DefaultMarkerSymbol.xaml的Silverlight资源字典文件,如下图:



2、打开DefaultMarkerSymbol.xaml文件,删掉里面的内容,拷贝如下的内容

<ControlTemplate
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
    xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
    >
    <Canvas>
        <vsm:visualstatemanager.VisualStateGroups>
            <vsm:VisualStateGroup x:Name="CommonStates">
                <vsm:VisualState x:Name="normal">
                    <Storyboard RepeatBehavior="Forever">
                        <DoubleAnimation BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleX)" From="1" To="5" Duration="00:00:01" />
                        <DoubleAnimation BeginTime="0:0:0" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)" From="1" To="5" Duration="00:00:01" />
                        <DoubleAnimation BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.Opacity)" From="1" To="0" Duration="00:00:01" />
                    </Storyboard>
                </vsm:VisualState>
            </vsm:VisualStateGroup>
        </vsm:visualstatemanager.VisualStateGroups>
        <Ellipse Height="25" Width="25" Canvas.Left="0" Canvas.Top="0" RenderTransformOrigin="0.5,0.5" x:Name="ellipse" IsHitTestVisible="False">
            <Ellipse.RenderTransform>
                <ScaleTransform />
            </Ellipse.RenderTransform>
            <Ellipse.Fill>
                <RadialGradientBrush>
                    <GradientStop Color="#00FF0000" />
                    <GradientStop Color="#FFFF0000" Offset="0.25" />
                    <GradientStop Color="#00FF0000" Offset="0.5" />
                    <GradientStop Color="#FFFF0000" Offset="0.75" />
                    <GradientStop Color="#00FF0000" Offset="1" />
                </RadialGradientBrush>
            </Ellipse.Fill>
        </Ellipse>
        <Ellipse Height="20" Width="20" Canvas.Left="0" Canvas.Top="0" Fill="#FFFF0000" x:Name="ellipse1" />
    </Canvas>
</ControlTemplate >

3、Silverlight程序代码中使用,这里是通过选中ListBox中的站点名称,进行聚焦显示方法如下:

 private void lbSearchSite_SelectionChanged(object sender,System.Windows.Controls.SelectionChangedEventArgs e)
        {
            RainFall findResult = e.AddedItems[0] as RainFall;
            Graphic g = new Graphic()
            {
                Geometry = mercator.FromGeographic(new MapPoint(double.Parse(findResult.Latitute.ToString().Trim()),double.Parse(findResult.Longitute.ToString().Trim())))
            };
            MarkerSymbol symbol = new MarkerSymbol();
            string contentStr = new StreamReader(
                         Application.GetResourceStream(
                                  new Uri("/MapClient;component/Template/DefaultMarkerSymbol.xaml",UriKind.Relative)
                                  ).Stream).ReadToEnd();
            symbol.ControlTemplate = (ControlTemplate)XamlReader.Load(contentStr);
            symbol.OffsetX = 10;
            symbol.OffsetY = 10;
            g.Symbol = symbol;

            ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = mercator.ToGeographic(g.Geometry).Extent; //选中点的位置
            double expandPercentage = 10;

            //加数值后,聚焦(这里需要注意,进行地理坐标和墨卡托坐标的转换)
            double widthExpand = (selectedFeatureExtent.Width + 5) * (expandPercentage / 100);
            double heightExpand = (selectedFeatureExtent.Height + 5) * (expandPercentage / 100);
            ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new Envelope(WKIDConvert.lonlat2mercator(new MapPoint(selectedFeatureExtent.XMin - (widthExpand / 2),selectedFeatureExtent.YMin - (heightExpand / 2))),WKIDConvert.lonlat2mercator(new MapPoint(selectedFeatureExtent.XMax + (widthExpand / 2),selectedFeatureExtent.YMax + (heightExpand / 2))))
            {
                SpatialReference = new SpatialReference(102100)
            };
            try
            {
                //聚焦
                myMap.ZoomTo(displayExtent);
                ShowFocus(g);
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// 显示聚焦点
        /// </summary>
        public void ShowFocus(Graphic g)
        {
            Graphicslayer graphicslayerSW = myMap.Layers["GraphicslayerRed"] as Graphicslayer;
            //产生红色光晕
            if (graphicslayerSW.Graphics.Count < 1)
            {
                graphicslayerSW.Graphics.Add(g);
            }
            else
            {
                graphicslayerSW.Graphics.RemoveAt(0);
                graphicslayerSW.Graphics.Add(g);
            }
        }


4、效果图如下:

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...