您可以创建一个自定义OverlayView来做到这一点:
var LayerOverlay = function () {
this.overlays = [];
}
LayerOverlay.prototype = new google.maps.OverlayView();
LayerOverlay.prototype.addOverlay = function (overlay) {
this.overlays.push(overlay);
};
LayerOverlay.prototype.updateOverlays = function () {
for (var i = 0; i < this.overlays.length; i++) {
this.overlays[i].setMap(this.getMap());
}
};
LayerOverlay.prototype.draw = function () {};
LayerOverlay.prototype.onAdd = LayerOverlay.prototype.updateOverlays;
LayerOverlay.prototype.onRemove = LayerOverlay.prototype.updateOverlays;
然后,一旦将叠加层添加到LayerOverlay
,您就可以仅使用一个来显示或隐藏它们setMap
:
var layer1 = new LayerOverlay();
layer1.addOverlay(createMarker());
layer1.addOverlay(createMarker());
layer1.addOverlay(createMarker());
// hide all markers
layer1.setMap(null);
// show all markers
layer1.setMap(map);