Document Maps JavaScript API Reference Manual Group LayerGroup

LayerGroup

AMap.LayerGroup

The LayerGroup class is used to wrap instances of other layer classes, performing batch operations on collections of instances, thereby avoiding the need for developers to loop through multiple layer instances that require the same properties to be set.

Moreover, once the setMap method is executed on a LayerGroup, any new layers added to the LayerGroup will automatically have their map property modified to the map corresponding to the group. Additionally, when a layer is removed from the group, it will also be removed from the map corresponding to the group.

If an event listener is added or removed from a layer collection, the layer collection will process all layer instances in the collection. As long as the layer supports the event, the event binding/removal will take effect on the layer.

Construct a LayerGroup object

new AMap.LayerGroup(layers: Array<Layer>)

Parameter

layers (Array<Layer>)Layer Array

Demo

var trafficLayer = new AMap.TileLayer.Traffic({
  zIndex: 11
});
var roadNetLayer = new AMap.TileLayer.RoadNet({
  zIndex: 10
});
var layerGroup = new AMap.LayerGroup([trafficLayer, roadNetLayer]);
layerGroup.setMap(map);

Method

setMap(map)

Add to the map

Parametermap (Map) Map object

return value: (objectObject containing layer type and other information

Demo:

layerGroup.setMap(map);

hasLayer(layer)

Determine if the incoming layer instance is in the collection

Parameterlayer (LayerLayer instance to be judged

return value: (boolean) Returns true if included, false if not

Demo:

layerGroup.hasLayer(roadNetLayer);

setOptions(opts)

Modify layer properties (including line style, color, etc.)

Parameteropts (LayerOptions) 

Attribute

Type

Description

visible

Boolean

Visibility

opacity

Number

Transparency

zIndex

Number

Level

zooms

Array<Number>

Visible range of the collection

return value:  (objectAn object containing information such as layer types

Demo:

layerGroup.setOptions({
  opacity: 0.5
});

eachLayer(iterator)

Perform iteration operations on the layers in the collection

Parameter: 

iterator (Function):function(layer, index, collections),The related meanings are as follows:

layer: The currently iterated layer

index: The sequence number of the layer in the collection (starting from 0)

collections: All layer instances

Demo:

layerGroup.eachLayer((layer, index, collections) => {
  console.log(layer, index, collections);
})

addLayer(layer)

Add an array of layers to the collection, duplicate layers are not supported

Parameterlayer (Layer) layer object

Demo:

var roadNetLayer = new AMap.TileLayer.RoadNet({
  zIndex: 10
});
layerGroup.addLayer(roadNetLayer);

addLayers(layers)

Add a single layer to the collection, duplicate layers are not supported

Parameterlayers (Array<Layer>) Layer array

Demo:

var trafficLayer = new AMap.TileLayer.Traffic({
  zIndex: 11
});
var roadNetLayer = new AMap.TileLayer.RoadNet({
  zIndex: 10
});
layerGroup.addLayers([trafficLayer,roadNetLayer]);

removeLayer(layer)

Remove the layer instance passed in from the collection

Parameterlayer (Layer) Layer object

Demo:

layerGroup.removeLayer(trafficLayer);

removeLayers(layers)

Remove the array of layer instances passed in from the collection

Parameterlayers(Array<Layer>) Layer array

Demo:

layerGroup.removeLayers([trafficLayer,roadNetLayer]);

getLayers()

Get all objects in the group, including layers and overlays

return value:  (Array<Layers>Instance object information contained in the LayerGroup array

Demo:

layerGroup.getLayers();

clearLayers()

Clear layer

Demo:

layerGroup.clearLayers();

hide()

Set layer hidden

Demo:

layerGroup.hide();

show()

Set layer visible

Demo:

layerGroup.show();

on(type, Event callback function)

Batch event binding

Parameter:

type (String)Event name, e.g.: complete

Event callback function (Function) 

Demo:

layerGroup.on('complete', (e) => {
  console.log(e);
})

reload()

Reload layer resources and re-render

return value:  (objectObject containing layer type information

Demo:

layerGroup.reload();