0

配列から多数のポリゴンをプロットし、それぞれに 3 つのイベント リスナーを追加しています。マウスオーバー、マウスアウト、クリックはすべて期待どおりに機能し、特定のポリゴンにのみ影響します。

必要なのは、クリックイベントが発生したときにマウスアウトイベントを無効にして、クリックで発生した色の変化がマウスアウトによって削除されないようにすることです。

ここや他の場所で多くの投稿を読んだ後、マウスアウト イベント リスナーにハンドルを追加してから、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');
                              });

( Seb Pのコメントで言及されているように、メソッドはclearListenersではなくと呼ばれることに注意してください)removeListeners

于 2013-06-14T14:29:54.913 に答える