0

I tried to add several Google Maps Engine layers using the following code:

gmeLayer = new google.maps.visualization.DynamicMapsEngineLayer({
    accessToken: token,
    layerId: layerId
});
gmeLayer.setMap(map);

I would like to display the ploygon layer on the bottom and point layer on the top.

How can I set the order for each layer?

1

I assume you have two individual GME Layers (vs a single layer with both points and polys).

They should be displayed in the order they were added to the map. So simply first add the polygon layer, and then after that has been loaded, add the point layer.

So if you have a few layers that need to be in the same order, and then you want to hide one in the middle and have it show up in the middle again, you'll want to first have put all layers in array when initializing the map. Maybe it would look something like this (untested, not optimized)

// state of layers after second layer is hidden
layers = [
  {name: layer1, visible: true, layer: layerObj},
  {name: layer2, visible: false, layer: layerObj},
  {name: layer3, visible: true, layer: layerObj}]

// make layer2 visible, and then call displayOrderedLayers()

function displayOrderedLayers() {
  for (var i=0; i<layers.length; i++) {
    layers[i].layer.setMap(null);  
  }
  for (var i=0; i<layers.length; i++) {
    if (layers[i].visible) {layers[i].layer.setMap(map);}  
  }
}
  • The problem is that I would like to show/hide a layer using the layer's setMap function. When a hidden layer is shown up, it is always on the top. Also, what is the event to track if a layer is fully loaded. – G Chen Jun 6 '14 at 17:55
  • Same logic applies: If you turn a layer on, and you want it not on the top, then you'll need to remove all of them and them re-add them in the order you want. Edited answer to show some logic. – jlivni Jun 6 '14 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.