0

我正在从一个数组中绘制多个多边形,然后为每个多边形添加三个事件侦听器;mouseover、mouseout 和 click 所有这些都按预期工作,并且只影响特定的多边形。

我需要的是在发生单击事件时禁用 mouseout 事件,以便单击时发生的颜色变化不会被 mouseout 删除。

在这里和其他地方阅读了很多帖子后,我尝试为 mouseout 事件侦听器添加句柄,然后添加 google.maps.event.removeListener(listentothis); 到点击侦听器,但它不起作用。

我也试过 google.maps.event.clearListeners(chartLayer, 'mouseout'); 但这也不行。

完整代码见http://testsite.imray.com/mapsv2.php

任何指导将不胜感激,因为它正在我的脑海中,我不明白为什么它不起作用。

谢谢

要求的代码:

        for (var i = 0; i < chartData.length; i++) {
            chartLayer  = new google.maps.Polygon({
                series: chartData[i].seriesID,
                paths: chartData[i].latLngs,
                map: map,
                strokeColor: chartData[i].colour1,
                strokeOpacity: 1,
                strokeWeight: 1,
                fillColor: chartData[i].colour2,
                fillOpacity: 0,
                activeOutline: chartData[i].colour3,
                activeInterior: chartData[i].colour4,
                itemcode: chartData[i].itemcode,
                itemname: chartData[i].itemname,
                itemlink: chartData[i].itemlink,
                itemscle: chartData[i].itemscle,
                itemedtn: chartData[i].itemedtn
            });
            imrayLayer.push(chartLayer);

            google.maps.event.addListener(chartLayer, 'mouseover', function() {
                this.setOptions({
                    strokeColor: this.activeOutline,
                    fillColor: this.activeInterior, 
                    fillOpacity: 0.5
                });
            });

            var listentothis = google.maps.event.addListener(chartLayer, 'mouseout', function() {
                this.setOptions({
                    strokeColor: this.strokeColor,
                    fillOpacity: 0
                });
            });

            google.maps.event.addListener(chartLayer, 'click', function() {
                google.maps.event.removeListener(listentothis);
                this.setOptions({
                    strokeColor: this.activeOutline,
                    fillColor: this.activeInterior, 
                    fillOpacity: 0.5
                });
                var text = '<h3>' + this.itemname + '</h3>';
                text = text + this.itemscle + this.itemedtn;
                text = text + '<p class="mbot5"><a href="' + this.itemlink + '" title="View full product details" target="_blank">View product page</a></p>';
                text = text + '<form action="" id="addForm" name="addForm"><input type="hidden" id="ItemCode" name="ItemCode" value="' + this.itemcode + '" /><p class="bold">Add to basket <a href="javascript:void(0);" onClick="addtobasket()" title="Add this chart to your basket" /><img src="files/images/buy-arrow.png" style="vertical-align:middle;" /></a></p></form>';
                text = text + '<div id="message"></div>';
                showInSideDiv(text);
            });

            chartLayer.setMap(map);
        }
4

1 回答 1

1

你会覆盖每个循环chartLayerlistentothis所以当你点击一个多边形时,它总是会影响循环内最后创建的多边形(或事件)。

您可以轻松地this在点击回调中使用来访问点击的多边形:

google.maps.event.addListener(chartLayer, 
                              'click', 
                              function() {
                                google.maps.event.clearListeners(this,'mouseout');
                              });

(请注意,该方法被称为clearListeners,而不是removeListenersSeb P 的评论中提到的)

于 2013-06-14T14:29:54.913 回答