在Google Map V3中,如何在多边形内部和上方放置标签?

tra*_*chy 15 label polygon google-maps-api-3

在Google Map V3中,如何在多边形内部和上方放置标签?在V2中没有标签覆盖当我使用库maplabel时,即使我指定了更高的Z-index,我也可以将文本放在里面,但不在上面.谢谢菲尔

Moh*_*ngh 7

太晚了,但我遇到了同样的问题,这段代码运行良好!

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);
    });
}
Run Code Online (Sandbox Code Playgroud)


小智 5

maplabel 在 mapPane 中的画布上呈现其文本,通常在 floatPane 中的所有 zIndex 值下方呈现。要将 mapPane 画布带入您的 zIndex 顺序,请尝试:

var mapLabel = new MapLabel({
    position: new google.maps.LatLng(yourLat, yourLng),
    text: 'test',
    zIndex: 101} );
$(mapLabel.getPanes().mapPane).css('z-index', mapLabel.zIndex);
Run Code Online (Sandbox Code Playgroud)


Jay*_*han 1

使用谷歌地图实用程序库

设置标签内容,找到多边形的中心位置,就是这样:)

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);
Run Code Online (Sandbox Code Playgroud)