Document Maps JavaScript API Reference Manual Group OverlayGroup

OverlayGroup

AMap.OverlayGroup

The OverlayGroup class is used to wrap instances of other overlay classes, performing operations on the collection of instances as a whole, avoiding the need for developers to loop through multiple overlay instances that require the same attributes. Additionally, when an overlay is removed from the group, it will also be removed from the map corresponding to the group. Currently, OverlayGroup supports Marker, Polygon, Polyline, Circle, CircleMarker, Rectangle, Ellipse, and BezierCurve.

Construct an OverlayGroup object

new AMap.OverlayGroup(overlays: Array<Overlay>)

Parameter

overlays(Array<Overlay>)Overlay array

Demo

//Create a Marker instance
const marker = new AMap.Marker({
  position: new AMap.LngLat(77.157683, 28.663787), //Latitude and longitude object, or a one-dimensional array composed of latitude and longitude
});
//Configure the polyline path
var path = [
  new AMap.LngLat(77.019667, 28.630042),
  new AMap.LngLat(77.063612, 28.602315),
  new AMap.LngLat(77.126097, 28.576992),
  new AMap.LngLat(77.179655, 28.554679),
];
//Create a Polyline instance
var polyline = new AMap.Polyline({
  path: path,
});
var overlayGroup = new AMap.OverlayGroup([marker, polyline]);
map.add(overlayGroup);

Method

addOverlay(overlay)

Add a single overlay to the collection, duplicate overlays are not supported

Parameteroverlay(OverlayInstance object of the overlay

Demo:

overlayGroup.addOverlay(marker);

addOverlays(overlays)

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

Parameteroverlays(Array<Overlay>) 覆盖物的实例对象数组

Demo:

overlayGroup.addOverlays([marker,polyline]);

getOverlays()

Return all overlays in the current collection

return value: (Array) All overlay information in the collection

Demo:

overlayGroup.getOverlays();

hasOverlay

Determine if the incoming overlay instance is in the collection

Parameteroverlay (Overlay) Overlay instance

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

Demo:

overlayGroup.hasOverlay(marker);

removeOverlay(overlay)

Remove the overlay instance passed in from the collection

Parameteroverlay (OverlayOverlay instance

Demo:

overlayGroup.removeOverlay(marker);

removeOverlays(overlays)

Remove the array of overlay instances passed in from the collection

Parameteroverlays  (Array<Overlay>) Array of overlay instance objects

Demo:

overlayGroup.removeOverlays([marker,polyline]);

clearOverlays()

Clear the collection

Demo:

overlayGroup.clearOverlays();

eachOverlay(iterator)

Perform iterative operations on the overlays in the collection

Parameter

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

 overlay Currently iterated overlay index: the sequence number of the overlay in the collection (starting from 0) collections: all overlay instances

Demo:

overlayGroup.eachOverlay((overlay, index, collections) => {
  console.log(overlay, index, collections);
});

show()

Display overlays in the collection on the map

Demo:

overlayGroup.show();

hide()

Hide overlays in the collection on the map

Demo:

overlayGroup.hide();

setOptions(opt)

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

Parameteropt (Object) Overlay properties

Demo:

overlayGroup.setOptions({
  strokeWeight: 2,
  strokeColor: "red",
});

Event

Event Name

Description

*(Events supported by the corresponding overlays in the collection

If you add or remove event listeners to an overlay collection, the collection will process all overlay instances in the collection. As long as the overlay supports the event, the event binding/removal will take effect on the overlay

For event object property descriptions, go to:MapsEvent