ArcGIS API ArcGISDynamicMapServiceLayer.setVisibleLayers对带有GroupLayer图层组的数据无效(针对LayerInfo)问题探讨

首先看下setVisibleLayers方法:

setVisibleLayers(ids, doNotRefresh?)
Sets the visible layers of the exported map. By default, the visible layers are as defined by the default visibility in LayerInfo. 
To display no visible layers specify an array with a value of -1. if(visible.length === 0){ visible.push(-1); } layer.setVisibleLayers(visible);

经过测试,JS开发中,setVisibleLayers对带有图层组的要素无法直接操作。

例如:

在加载后 对图层 LayerInfo(红圈,为GroupLayer)进行show/hide,无法正常操作。

解决方法1:

  发布图层时不要增加图层组(GroupLayer),所有图层都在一层之下,发布后调用即可正常使用;

解决方法2:

  第一种解决方法需把所有数据放同一层,不设置图层组,可针对简单数据,但数据繁多需要分组时,就需要考虑分组问题,GroupLayer本身在JS API加载后也是LayerInfo,

思路是:把VisibleLayer里面的图层组都删掉,再调用setVisibleLayers(visibleLayer)。

 1 function getVisibleLayers(layer, subLayerIndex) {
 2     require(["dojo/_base/array"],function(array) {
 3         var layerInfos = layer.layerInfos;
 4         var i;
 5         // array for setting visible layers
 6         var visibleLayers = [-1];
 7 
 8         if (typeof subLayerIndex !== "undefined") {
 9             var newVis = !layerInfos[subLayerIndex].defaultVisibility;
10             // reverse current visibility of sublayer
11             layerInfos[subLayerIndex].defaultVisibility = newVis;
12         }
13 
14         // for each sublayer
15         for (i = 0; i < layerInfos.length; i++) {
16             var info = layerInfos[i];
17             // push to visible layers if it's visible
18             if (info.defaultVisibility) {
19                 visibleLayers.push(info.id);
20                 var negative = array.lastIndexOf(visibleLayers, -1);
21                 if (negative !== -1) {
22                     visibleLayers.splice(negative, 1);
23                 }
24             }
25         }
26         //Now that the array of visibleLayer Ids is assembled,说明:此处即是对GroupLayer进行处理
27         //strip off Ids of invisible child layers, and
28         //Ids of group layers (group layer Ids should not be submitted
29         //in .setVisible() or loss of toggle control madness ensues.
30         //Remove layers whos parents are not visible:
31         var noInvisibleParents = [];
32         for (i = 0; i < visibleLayers.length; i++) {
33             var id = visibleLayers[i];
34             var hasParentsInVisibleArray = this._allIdsPresent(layer, id, visibleLayers);
35             if (hasParentsInVisibleArray) {
36                 noInvisibleParents.push(id);
37             }
38         }
39         var noGroups = [];
40         for (var j = 0; j < noInvisibleParents.length; j++) {
41             var lyrInfo = this._getLayerInfo(layer, noInvisibleParents[j]);
42             if (lyrInfo && lyrInfo.subLayerIds === null) {
43                 noGroups.push(noInvisibleParents[j]);
44             }
45         }
46         // note: set -1 if array is empty.
47         if (!noGroups.length) {
48             noGroups = [-1];
49         }
50         return noGroups;
51     });
52 }

  代码略显复杂,不过对于自定义图层操作可做参考。

  注:参考ArcGIS JS API的LayerList源码

posted @ 2015-10-15 17:18  GIS流水  阅读(3210)  评论(0编辑  收藏  举报