In Google Map V3, how to put a label inside and above a polygon?

trachy picture trachy · Dec 19, 2012 · Viewed 23k times · Source

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

Answer

Mohan Singh picture Mohan Singh · Jun 27, 2017

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);
    });
}