ArcGIS API for Silverlight程式碼中使用Template模板

暖楓無敵發表於2014-11-07

       在專案開發中,會遇到點選中聚焦閃爍效果,但是因為在使用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、效果圖如下:



相關文章