谷歌地图V3绘制多边形并打开信息窗口



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

这是我的所有代码;

       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参数,具有用作锚点的公共position属性。在这种情况下,不需要提供定位点,因为您已经调用了传递google.maps.LatLnginfowin.setPosition 方法。从open方法的google.maps.InfoWindow api-doc 描述中:

在给定地图上打开此信息窗口。(可选)信息窗口可以 与定位点关联。在核心 API 中,唯一的锚点是 标记类。但是,定位点可以是任何公开 位置属性和可选的锚点,用于计算 pixelOffset (请参阅 InfoWindowOptions)。锚点是偏移量 从锚点的位置到信息窗口的尖端。

最新更新