Graphic is already associated with another layer


    在完成历史演变功能时出现如下情况,即:按照年份初始化图层,在每个初始化的图层中添加要素时报错(要素的Attributes相同)

    报错的核心代码如下(注:此处的CustomLayer是继承GraphicsLayer,可直视为GraphicsLayer
                                               此处的myMap是控件Map的ID)

    //历史演变第一年,此时没有报错
    CustomLayer customLayer = new CustomLayer();
    customLayer.Map = RenderEnty.myMap;
    customLayer.ID = "LSYB2012"; 
    customLayer.Visible = true;
    myMap.Layers.Add(customLayer);

    Graphic graphic  = (from g in listGraphic where graphic.Attributes["key"].ToString() == strCode select g).FirstOrDefault();
    graphic.Symbol = symbol;
    customLayer.Graphics.Add(graphic); 

    //历史演变第二年,此时在红色部分报错
    CustomLayer customLayer = new CustomLayer();
    customLayer.Map = RenderEnty.myMap;
    customLayer.ID = "LSYB2013"; 
    customLayer.Visible = true;
    myMap.Layers.Add(customLayer);

    Graphic graphic  = (from g in listGraphic where graphic.Attributes["key"].ToString() == strCode select g).FirstOrDefault();
    graphic.Symbol = symbol;
      customLayer.Graphics.Add(graphic); 

    开始以为这里的问题是由于graphic的属性字段和值一样报错的,于是果断将两次Graphic生成做调整。

    Graphic graphic  = (from g in listGraphic where graphic.Attributes["key"].ToString() == strCode select g).FirstOrDefault();
     graphic.Attributes.Add("id",DateTime.Now.ToString("yyyymmddHHmmssfff"));
     graphic.Symbol = symbol;
     customLayer.Graphics.Add(graphic); 

    此时,在第二次执行红色部分的时候就报错,提示“已经添加了相同项”,跟踪代码发现在graphic中已经存在了id这个属性字段。即使对id这个属性字段进行是否包含与添加修改字段值的操作,依旧报错第一个。再次将两次Graphic生成做调整。

    Graphic graphic  = (from g in listGraphic where graphic.Attributes["key"].ToString() == strCode select g).FirstOrDefault();

    Graphic newGraphic = graphic;
    newGraphic.Symbol = symbol;
     customLayer.Graphics.Add(newGraphic); 

    此时,依旧报错第一个。 再次将两次Graphic生成做调整。 

    Graphic graphic  = (from g in listGraphic where graphic.Attributes["key"].ToString() == strCode select g).FirstOrDefault();

    Graphic newGraphic = new Graphic();
    newGraphic.Symbol = symbol;
    newGraphic.Geometry = graphic.Geometry; 
     customLayer.Graphics.Add(newGraphic);  

    至此,问题解决。但为何保证Graphic要素中的属性字段值不一致的绘制在不同图层依旧报错,如有懂得原理的请多指点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值