Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing a MapLabel on top of a Polygon in Google Maps V3

I'm trying to place a MapLabel on top of a Polygon in Google Maps V3. I've tried to set the MapLabel zIndex to 2 and the Polygon zIndex to 1 without any luck. Isn't this possible since the Polygon doesn't really follow zIndex?

I've created a jsFiddle for you guys to check out: http://jsfiddle.net/7wLWe/1/

Solution: In maplabel.js change:

mapPane.appendChild(canvas);

to:

floatPane.appendChild(canvas);

The reason is because floatPane is above all map layers (pane 6)

http://code.google.com/apis/maps/documentation/javascript/reference.html#OverlayView

like image 509
John Avatar asked Feb 07 '12 22:02

John


People also ask

How do I edit a polygon on Google Maps?

Editing a polygon Once you have created your polygon it will appear in the 'Temporary Places' area in the Places panel. You can edit the polygon. 8. Right click on the polygon name in the Places panel.


2 Answers

This is probably a late find.. but hope someone would find this useful.

If you don't want to use floatPane (John's Solution) as this will be always on top of everything, and want to give a custom zIndex.. Edit the maplabel.js. Add the following line just before the end of MapLabel.prototype.onAdd = function() {

if (canvas.parentNode != null) {
    canvas.parentNode.style.zIndex = style.zIndex;
}

Now you can pass zIndex while creating a maplabel:

var mapLabel = new MapLabel({
    text: "abc",
    position: center,
    map: map,
    fontSize: 8,
    align: 'left',
    zIndex: 15
});
like image 154
Avishek Avatar answered Oct 31 '22 09:10

Avishek


If you don't want to touch maplabel.js, you can add this function to change the z-index of the parent of the labels (although if you have other canvas elements, you may need to alter the function):

//change the z-index of the canvas elements parents to move them above polygon layer
google.maps.event.addListenerOnce(map, 'idle', function(){
//var canvasElements = document.getElementsByTagName('canvas');//plain js to get the elements
var canvasElements = jQuery('canvas'); //jquery for easy cross-browser support
    for(var i=0; i<canvasElements.length; i++){
        canvasElements[i].parentNode.style.zIndex = 9999;
    }

});
like image 23
brouxhaha Avatar answered Oct 31 '22 09:10

brouxhaha