This is regarding Google Maps API v3:
When you draw various Polylines
, Polygons
, MapLabels
or any of the other Overlay elements in a custom Google Map, it draws them on the mapPane
. Is it possible to somehow create multiple custom layers that you can draw Overlay objects on and then easily enabled/disable (show/hide) those layers?
I see documentation about layers here: https://developers.google.com/maps/documentation/javascript/layers
But nothing about custom layers.
Custom Map Layers When you create a new custom map in Google Maps, a new “Untitled Layer” is added by default. You can add as many layers as you want to your custom map, allowing you to separate the different components of your new map from each other, by clicking the “Add layer” button.
The Google Maps APIs now support you in creating beautiful styled maps for your Android and iOS apps as well as your website using the same JSON style object.
You won't be charged until your usage exceeds $200 in a month. Note that the Maps Embed API, Maps SDK for Android, and Maps SDK for iOS currently have no usage limits and are at no charge (usage of the API or SDKs is not applied against your $200 monthly credit).
All Maps Embed API requests are available at no charge with unlimited usage.
You can create a custom OverlayView to do that :
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;
Then once added your overlays to LayerOverlay
, you can show or hide them only with one 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With