Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Google Maps Layers via API?

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.

like image 812
Jake Wilson Avatar asked Dec 04 '12 08:12

Jake Wilson


People also ask

Can you add custom layers to Google Maps?

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.

Can you customize Google Maps API?

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.

Is Google Maps API no longer free?

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).

Is Google Maps Embed API free?

All Maps Embed API requests are available at no charge with unlimited usage.


1 Answers

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);
like image 147
Alexandre Ardhuin Avatar answered Oct 05 '22 18:10

Alexandre Ardhuin