首页 / 浏览问题 / WebGIS / 问题详情
显示隐层麻点图
20EXP 2023年09月06日

使用产品:iserver 11i 操作系统:win10x64
数据类型: postgre sql
问题详细描述:1、假设我使用一个复选框控制麻点图的显示和隐藏,checkbox选中时我的代码是:new L.supermap.TiledMapLayer(this.url,{transparent: true, noWrap: true}).addTo(this.map),那我取消选中时应该怎么隐藏麻点图呢?
问题重现步骤: 1.

1个回答

您好,可以将该图层赋值给一个变量,当取消选中时this.map.removeLayer(变量);
710EXP 2023年09月06日
怎么赋值呢,this.url不算赋值吗
将该图层添加到地图上,并将其赋值给一个变量var tiledLayer = new L.supermap.TiledMapLayer(this.url,{transparent: true, noWrap: true}).addTo(this.map);
这个成功了,但是地图叠加两层麻点图之后就遇到问题了,声明了两个变量,赋值两个麻点图:1、两层麻点图先取消第二层麻点图可以取消,之后第一层麻点图就取消不了。2、两层麻点图先取消第一层麻点的话,两层图哪个都取消不了

代码如下:

<el-checkbox-group v-model="checklist" @change="layerTools"><el-checkbox label="first">一号麻点</el-checkbox><el-checkbox label="second">二号麻点</el-checkbox></el-checkbox-group>

layerTools(val) {

if (val.includes('first')) { this.onelayer = new L.supermap.TiledMapLayer(this.oneurl,{transparent: true, noWrap: true}).addTo(this.map); } else { if (this.onelayer != null) { this.map.removeLayer(this.onelayer) } }

if (val.includes('second')) { this.twolayer = new L.supermap.TiledMapLayer(this.twourl,{transparent: true, noWrap: true}).addTo(this.map); } else { if (this.twolayer != null) { this.map.removeLayer(this.twolayer) } }

}

每次选中或取消选择时,都会创建新的 L.supermap.TiledMapLayer 对象并赋值给 this.onelayerthis.twolayer,但是移除图层时却只尝试移除当前对象的引用。这就导致了取消第二层麻点图后无法正确移除第一层麻点图,并且无法取消两层麻点图。

在修正后的代码中,每次选中或取消选择时,首先检查对应的图层对象是否已经存在,只有当不存在时才创建新的图层并添加到地图上。同时,在取消选择时,移除图层后将对应的图层变量设置为 null。 

 layerTools(val) {
      if (val.includes('first')) {
        if (this.onelayer == null) {
          this.onelayer = new L.supermap.TiledMapLayer(this.oneurl, {
            transparent: true,
            noWrap: true
          }).addTo(this.map);
        }
      } else {
        if (this.onelayer != null) {
          this.map.removeLayer(this.onelayer);
          this.onelayer = null;
        }
      }

      if (val.includes('second')) {
        if (this.twolayer == null) {
          this.twolayer = new L.supermap.TiledMapLayer(this.twourl, {
            transparent: true,
            noWrap: true
          }).addTo(this.map);
        }
      } else {
        if (this.twolayer != null) {
          this.map.removeLayer(this.twolayer);
          this.twolayer = null;
        }
      }
    }
  }

...