Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 643
trachy Avatar asked Dec 19 '12 21:12

trachy


3 Answers

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);
like image 175
Andrew Avatar answered Nov 18 '22 22:11

Andrew


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);
    });
}
like image 7
Mohan Singh Avatar answered Nov 18 '22 22:11

Mohan Singh


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);
like image 1
Jaykishan Avatar answered Nov 18 '22 22:11

Jaykishan