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
Parameter: map (Map) Map object
return value: (object) Object containing layer type and other information
Demo:
layerGroup.setMap(map);hasLayer(layer)
Determine if the incoming layer instance is in the collection
Parameter: layer (Layer) Layer 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.)
Parameter: opts (LayerOptions)
return value: (object) An 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
Parameter: layer (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
Parameter: layers (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
Parameter: layer (Layer) Layer object
Demo:
layerGroup.removeLayer(trafficLayer);removeLayers(layers)
Remove the array of layer instances passed in from the collection
Parameter: layers(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: (object) Object containing layer type information
Demo:
layerGroup.reload();