In Google Map V3, how to put a label inside and above a polygon? There's no label overlay as in V2 When I use the library maplabel, I can put the text inside, but not above, even if I specify an higher Z-index. Thanks Phil
maplabel renders its text on a canvas in the mapPane, which normally renders below all zIndex values in the floatPane. To bring the mapPane canvas into your zIndex order, try:
var mapLabel = new MapLabel({
position: new google.maps.LatLng(yourLat, yourLng),
text: 'test',
zIndex: 101} );
$(mapLabel.getPanes().mapPane).css('z-index', mapLabel.zIndex);
It's too late but I have faced the same problem and this code works fine!
var triangleCoords = [
{ lat:30.655993, lng: 76.732375 },
{ lat: 30.651379, lng: 76.735808},
{ lat: 30.653456, lng: 76.729682}
]
var bermudaTriangle = new google.maps.Polygon({
paths: triangleCoords ,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
attachPolygonInfoWindow(bermudaTriangle)
function attachPolygonInfoWindow(polygon) {
var infoWindow = new google.maps.InfoWindow();
google.maps.event.addListener(polygon, 'mouseover', function (e) {
infoWindow.setContent("Polygon Name");
var latLng = e.latLng;
infoWindow.setPosition(latLng);
infoWindow.open(map);
});
}
Use google-maps-utility-library
Set label content, find center position of your polygon and thats it :)
var labelOptions = {
content: label,
boxStyle: {
textAlign: "center",
fontSize: "8pt",
width: "50px"
},
disableAutoPan: true,
pixelOffset: new google.maps.Size(-25, 0),
position: this.my_getBounds(gpoints).getCenter(),
closeBoxURL: "",
isHidden: false,
pane: "mapPane",
enableEventPropagation: true
};
var polygonLabel = new InfoBox(labelOptions);
polygonLabel.open(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