在arcgis api for javascript 3.43中,我有一个地图服务和四个要素服务,我把它们作为不同的图层加载在地图上,现在我要怎么控制这些图层的显示呢?
时间: 2024-05-13 17:18:31 浏览: 90
【JavaScript源代码】arcgis.js控制地图地体的显示范围超出区域自动弹回(实现思路).docx
您可以使用ArcGIS API for JavaScript 3.43中的`setVisibleLayers()`方法来控制要素图层的显示。
以下是一个示例代码,用于控制地图服务和四个要素服务的显示:
```javascript
//加载地图服务
var mapServiceLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://yourserver/arcgis/rest/services/YourMapService/MapServer");
map.addLayer(mapServiceLayer);
//加载四个要素服务
var featureLayer1 = new esri.layers.FeatureLayer("http://yourserver/arcgis/rest/services/YourFeatureService1/FeatureServer/0");
var featureLayer2 = new esri.layers.FeatureLayer("http://yourserver/arcgis/rest/services/YourFeatureService2/FeatureServer/0");
var featureLayer3 = new esri.layers.FeatureLayer("http://yourserver/arcgis/rest/services/YourFeatureService3/FeatureServer/0");
var featureLayer4 = new esri.layers.FeatureLayer("http://yourserver/arcgis/rest/services/YourFeatureService4/FeatureServer/0");
map.addLayers([featureLayer1, featureLayer2, featureLayer3, featureLayer4]);
//设置默认显示的图层
var visibleLayers = [0, 1, 2, 3, 4];
mapServiceLayer.setVisibleLayers(visibleLayers);
featureLayer1.setVisibility(false);
featureLayer2.setVisibility(false);
featureLayer3.setVisibility(false);
featureLayer4.setVisibility(false);
//添加控件用于控制图层显示
var layerList = new esri.dijit.LayerList({
map: map,
layers: [mapServiceLayer, featureLayer1, featureLayer2, featureLayer3, featureLayer4]
}, "layerListDiv");
layerList.startup();
//定义事件处理程序,当用户选择要显示的图层时,将其设置为可见状态
dojo.connect(layerList, "onLayerClick", function(item) {
var visibleLayers = [];
dojo.forEach(layerList.getLayerVisibleItems(), function(layer) {
visibleLayers.push(layer.id);
});
mapServiceLayer.setVisibleLayers(visibleLayers);
});
```
在上面的示例中,首先加载地图服务和四个要素服务,并将它们作为不同的图层添加到地图中。然后,通过`setVisibleLayers()`方法设置默认显示的图层,并将所有要素图层的可见性设置为false。接下来,添加一个控件用于控制图层的显示,并定义事件处理程序,当用户选择要显示的图层时,将其设置为可见状态。
阅读全文