javascript - 谷歌地图V3绘制多边形并打开InfoWindow

标签 javascript google-maps google-maps-api-3

我正在动态绘制多边形,然后单击多边形以打开信息窗口。我正在成功绘制多边形,但单击信息窗口似乎没有。没有给出错误,只是看起来没有!

这是我的所有代码;

       function ADD_EVENT_FOR_POLYLINE_AND_POLYGON () {
            GLOBALS.PolyGonPath = new google.maps.MVCArray;
            GLOBALS.PolyGon = new google.maps.Polygon({
                strokeWeight: 3,
                fillColor: '#5555FF'
            });
            GLOBALS.PolyGon.setMap(GLOBALS.Map);
            google.maps.event.addListener(GLOBALS.Map, 'click', DRAW_POLYGON);
        }


     function DRAW_POLYGON(event) {
        GLOBALS.PolyGonPath.insertAt(GLOBALS.PolyGonPath.length, event.latLng);
        var marker = new google.maps.Marker({
            position: event.latLng,
            map: GLOBALS.Map,
            draggable: true
        });
        GLOBALS.PolyMarkers.push(marker);
        marker.setTitle("#" + GLOBALS.PolyGonPath.length);
        google.maps.event.addListener(marker, 'click', function () {
            marker.setMap(null);
            for (var i = 0, I = GLOBALS.PolyMarkers.length; i < I && GLOBALS.PolyMarkers[i] != marker; ++i);
            GLOBALS.PolyMarkers.splice(i, 1);
            GLOBALS.PolyGonPath.removeAt(i);
        });

        google.maps.event.addListener(marker, 'dragend', function () {
            for (var i = 0, I = GLOBALS.PolyMarkers.length; i < I && GLOBALS.PolyMarkers[i] != marker; ++i);
            GLOBALS.PolyGonPath.setAt(i, marker.getPosition());
        });

        **Here is I am adding a method to polygon for infowindow**
        GLOBALS.PolyGon.setPaths(new google.maps.MVCArray([GLOBALS.PolyGonPath]));
        google.maps.event.addListener(GLOBALS.PolyGon, 'click', SHOW_INFO);
    },


     function SHOW_INFO (event) {
        var infowin = new google.maps.InfoWindow();
        var vertices = GLOBALS.PolyGon.getPath();
        var contentString = "<b>Test</b><br />";

        for (var i = 0; i < vertices.length; i++) {
            var xy = vertices.getAt(i);
            contentString += "<br />" + "Coordinats: " + i + "<br />" + xy.lat() + "," + xy.lng();
        }
        infowin = new google.maps.InfoWindow();
        infowin.setContent(contentString);
        infowin.setPosition(event.latLng);
        infowin.open(GLOBALS.Map, this);
    }

最佳答案

尝试将 SHOW_INFO 函数中的最后一行代码更改为:

infowin.setPosition(event.latLng); //Leave this line
infowin.open(GLOBALS.Map, this);

至:

infowin.setPosition(event.latLng); //This line stays the same
infowin.open( GLOBALS.Map );

infowin.open 的第二个参数是一个可选的 MVCObject 参数,具有公共(public) position 属性,用作 anchor 。在这种情况下,您不需要提供 anchor ,因为您已经调用 infowin.setPosition 方法并传递 google.maps.LatLng。来自 google.maps.InfoWindow api-doc open 方法的描述:

Opens this InfoWindow on the given map. Optionally, an InfoWindow can be associated with an anchor. In the core API, the only anchor is the Marker class. However, an anchor can be any MVCObject that exposes the position property and optionally anchorPoint for calculating the pixelOffset (see InfoWindowOptions). The anchorPoint is the offset from the anchor's position to the tip of the InfoWindow.

关于javascript - 谷歌地图V3绘制多边形并打开InfoWindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11011630/

相关文章:

Javascript 网页期望至少加载一张 map ,但实际上无法加载任何内容

javascript - Meteor:按文本属性删除不起作用

javascript - 当多个 glob 时 gulp src 会重复

javascript - 如何调整羽毛笔编辑器的大小以适应 <div>?

javascript - 如何生成包含非零元素索引的张量?

javascript - 在javascript中动态创建类似谷歌地图的 map 控件

ios - 在 GMSPoligon 中点击时更改打印文本。 swift

javascript - 如果与 GeoJSON 层一起使用, map 标签的 zIndex 无效

android - 使用 android map v2 的 3d map View

google-maps-api-3 - 谷歌地图 v3 - 突出显示州或州的地区