微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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、效果图如下:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐