ArcGIS 添加 MarkerSymbol 弹出“图形符号无法序列化为 JSON”错误

  今天在做一个demo,向自定义图层中添加MarkerSymbol的时候,弹出“图形符号无法序列化为 JSON”错误,之前都没有出现过这个问题,我们首先来看一看我是怎样去添加图层,然后向图层中添加Graphic的,这个比较简单,直接贴出相关代码。

        GraphicsLayer carLayer = new GraphicsLayer();
            carLayer.ID = "CarLayer";
            carLayer.Renderer = new SimpleRenderer()
            {
                Symbol = this.Resources["Medium"] as ESRI.ArcGIS.Client.Symbols.MarkerSymbol                
            };            
            Graphic carGraphic = new Graphic();
            carGraphic.Geometry = new ESRI.ArcGIS.Client.Geometry.MapPoint(Convert.ToDouble("12697297.9815139"), Convert.ToDouble("2577264.46557406"), new ESRI.ArcGIS.Client.Geometry.SpatialReference(102100));  
            carLayer.Graphics.Add(carGraphic);
            MyMap.Layers.Add(carLayer);

  再看看我们前台定义的这个资源。

 <esri:MarkerSymbol x:Key="Medium" OffsetX="30" OffsetY="64">
            <esri:MarkerSymbol.ControlTemplate>
                <ControlTemplate>
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver">
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected" />
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImageNormal">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImageSelected">
                                            <DiscreteObjectKeyFrame KeyTime="0"  Value="{x:Static Visibility.Visible}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Image Name="ImageNormal" Source="Resoues/medium_policecar.png"  Visibility="Visible"/>
                        <Image Name="ImageSelected" Source="Resoues/medium_policecar_hit.png" Visibility="Hidden"/>
                    </Grid>
                </ControlTemplate>
            </esri:MarkerSymbol.ControlTemplate>
        </esri:MarkerSymbol>    

  这段代码结构也非常清楚,就是定义当前图层的Symbol,点击的时候会更换成另外的一张图片,以前在每一次使用的时候都不会出现什么问题,那么出现了问题首先看看是怎样一步一步去找到问题的?

      既然图层使用MarkerSymbol不行,那换一种试一下?换成TextSymbol试试看,更改代码如下:      

carLayer.Renderer = new SimpleRenderer()
            {
                    Symbol =new ESRI.ArcGIS.Client.Symbols.TextSymbol()
                   {
                       Text = "CAR",
                       FontSize = 48,
                       Foreground = Brushes.Red
                     }
            };     

  查看运行结果,成功! 

  那么是否是文件的路径不对,找不到图片的路径?

  仔细看图片的属性:通过VS2012的视图--》属性窗口来查看属性,无论是Source还是其它属性都是正常的,那么路径还是不对吗?干脆直接在代码里面写这个MarkerSymbol,然后将路径写死,这样总不会出现问题了吧?  

       GraphicsLayer carLayer = new GraphicsLayer();
            carLayer.ID = "CarLayer";
ESRI.ArcGIS.Client.Symbols.MarkerSymbol markSymbol = new ESRI.ArcGIS.Client.Symbols.MarkerSymbol();
        ControlTemplate ct = new ControlTemplate(); FrameworkElementFactory rootFactory = new FrameworkElementFactory(typeof(Grid)); FrameworkElementFactory imageFactory = new FrameworkElementFactory(typeof(Image)); imageFactory.SetValue(Image.StretchProperty, Stretch.Uniform); imageFactory.SetValue(Image.SourceProperty, new BitmapImage(new Uri(@"C:UsersyeboDesktopResizeInfoWindowResizeInfoWindowResizeInfoWindowExResouesmedium_policecar_hit.png", UriKind.Absolute))); rootFactory.AppendChild(imageFactory); ct.VisualTree = rootFactory; markSymbol.ControlTemplate = ct;         carLayer.Renderer = new SimpleRenderer() { Symbol = markSymbol }; Graphic carGraphic = new Graphic(); carGraphic.Geometry = new ESRI.ArcGIS.Client.Geometry.MapPoint(Convert.ToDouble("12697297.9815139"), Convert.ToDouble("2577264.46557406"), new ESRI.ArcGIS.Client.Geometry.SpatialReference(102100)); carLayer.Graphics.Add(carGraphic); MyMap.Layers.Add(carLayer);

  编译程序,继续报错。      

  继续寻找问题,路径不会有问题,MarkerSymbol的样式也没有问题?那么地图有问题吗?

      最后在 <esri:Map x:Name="MyMap" UseAcceleratedDisplay="True">中发现了这个属性,UseAcceleratedDisplay到底是做什么用的?去掉以后,再次运行,一切正常,同时如果将该属性改为false,代码也都运行正常,问题找到了,那么后面的思考不能少?这样才能够真正去掌握一门知识!      

   看到下面的几篇帖子介绍的很不错,就当去了解ArcGIS的知识,感兴趣的可以去认认真真看一下,然后去思考去总结。

   http://blog.csdn.net/arcgis_all/article/details/8215144

 http://blog.csdn.net/arcgis_all/article/details/40400331

   今天就将这些,以后再进一步去完善这些内容。

原文地址:https://www.cnblogs.com/seekdream/p/6074959.html