【问题标题】:GooglemapV3 infowindow closeclick event is not getting fireGooglemapV3 infowindow closeclick 事件没有被触发
【发布时间】:2016-05-20 13:56:44
【问题描述】:

以下代码用于在点击标记时在 Google 地图上显示信息窗口。但由于某些原因,信息窗口的“closeclick”事件不起作用。虽然早些时候它可以工作,但由于更多复杂的需求,如 infowindow 对象需要重新打开,因为它是在刷新 Ajax 调用上的地图之后。因此地图会在一分钟后保持刷新,并且要求如果在 Ajax 调用时没有关闭信息窗口,则信息窗口应保持打开状态。

我们还在地图上绘制折线,并在它在 Google 地图上绘制的每个点上打开一个信息窗口,同样的要求也适用于上述信息窗口对象。

        $(".mainDiv").each(function () {
    google.maps.event.trigger(map, 'click');
    if (PlayBackDevices.indexOf(removeSpaces($(this).find(".deviceid").html()).trim()) > -1) {
            var image = new google.maps.MarkerImage($(this).find(".imagepath").html());
            myLatLng = new google.maps.LatLng($(this).find(".latitude").html(), $(this).find(".longitude").html());
            var beachMarker = new MarkerWithLabel({
                position: myLatLng,
                map: map,
                icon: image,
                title: $(this).find(".deviceName").html().trim(),
                labelContent: $(this).find(".deviceName").html().trim()
            });
            markers.push(beachMarker);
            var imgPath = trailImagePath + trailColor.trim() + ".png";
            var beachMarkerTemp = new RichMarker({
                position: myLatLng,
                map: map,
                draggable: false,
                flat: true,
                anchor: RichMarkerPosition.BOTTOM//,
            });
            i = i + 1;
            oms.addListener('click', function (beachMarker) {
                infoWindow.close();
                infoWindowDevicePoints.length = 0;
                if (deviceName.trim() == beachMarker.title.trim()) {
                    $.ajax({
                        type: "Post",
                        url: "/Home/CommonLevel2Information",
                        data: { DeviceId: 101 },
                        async: true,
                        dataType: "html",
                        cache: false,
                        success: function (result) {
                            result = createInfo('', result + '<br/><a href="' + linkToNextLevel + '" title="Click to view DeviceDetail"><%=GlanceWeb.Resources.Level2.Level2.RegMrDtl%>...</a>', deviceId);
                            beachMarker.desc = result;
                            if (result.toString().indexOf("divMainSnap") > 0) {
                                var checkExist = setInterval(function () {
                                    $(".gm-style-iw").css("width", "400px !important;");
                                    infoWindow = new google.maps.InfoWindow({ content: beachMarker.desc, maxWidth: 400 });
                                    infoWindow.open(map, beachMarker);
                                    position = beachMarker.position;// iw.getPosition();
                                    $(".gm-style-iw").each(function () {
                                        if ($(this).find("div.divFirstSnap").length) {
                                            $(this).removeClass("wiThouImage");
                                            $(this).css("max-width", "none");
                                        }
                                    })
                                    clearInterval(checkExist);
                                }, 1000);
                            }
                            else {
                                var checkExist = setInterval(function () {
                                    infoWindow = new google.maps.InfoWindow({ content: beachMarker.desc, maxWidth: 200 });
                                    infoWindow.open(map, beachMarker);
                                    position = beachMarker.position;// iw.getPosition();
                                    if ($(".gm-style-iw").length > 0) {
                                        $(".gm-style-iw").removeAttr("width");
                                        $(".gm-style-iw").each(function () {
                                            if (!$(this).find("div.divFirstSnap").length) {
                                                $(this).addClass("wiThouImage");
                                            }
                                        })

                                        clearInterval(checkExist);
                                    }

                                }, 1000);
                            }

                            //Following section is used to manage device pop-ups after ajax 
                            for (var i = 0; i < infoWindowDevicePoints.length; i++) {
                                infoWindowDevicePoints[i].latLng
                                if (position == infoWindowDevicePoints[i].latLng)
                                    isExist = false;
                            }

                            if (isExist) {
                                infoWindowDevicePoints.push({
                                    latLng: beachMarker.position,
                                    popUpData: beachMarker.desc
                                });
                            }
                            UnBlockGlanceScreen();
                        }
                    });
                    }
            });
            google.maps.event.addListener(infoWindow, "closeclick", function () {
                debugger;

                var that = this;
                that.getposition()
                var latlnginfo = that.getposition();
                infowindowdevicepoints = $.grep(infowindowdevicepoints, function (value) {
                    return value.latlng != latlnginfo;
                });
            });

                oms.addListener('spiderfy', function () {
                    infoWindow.close();
                    infoWindowDevicePoints.length = 0;
                });
                oms.addMarker(beachMarker);

            }

        });


});

提前感谢您提供的任何帮助。

【问题讨论】:

  • 您在 5 个不同的地方添加了“closeclick”事件监听器。他们中的任何一个会被处决吗?您的代码相当复杂,超过 400 行。你能把这个问题简化为MCVE吗?
  • 你好 Duncan,我已经缩短了代码行数,以便更清楚地理解。 Infowindow 会打开,但 closeclick 事件不会触发。我试图将事件保存在不同的地方,以检查它是否在那里不起作用,但徒劳无功。此外,还有许多其他地方会在不同条件下弹出信息窗口。
  • 您在 ajax 成功处理程序中创建信息窗口。您是否已经将其定义为全局变量?在您附加 closeclick 事件处理程序时,如果您只执行console.log(infoWindow),您会得到什么?
  • 是的,infowindow 定义为全局变量。我试过做同样的 console.log(infoWindow) 什么都没做?

标签: javascript jquery ajax google-maps infowindow


【解决方案1】:

问题是您只是创建 infoWindow 以响应 ajax 请求。但是,当您添加 closeclick 事件侦听器时,它可能会在 ajax 响应发生之前执行。

你需要移动这个块:

google.maps.event.addListener(infoWindow, "closeclick", function() {
    debugger;

    var that = this;
    that.getposition()
    var latlnginfo = that.getposition();
    infowindowdevicepoints = $.grep(infowindowdevicepoints, function(value) {
        return value.latlng != latlnginfo;
    });
});

... 在这个块中,在创建 infoWindow 的 if-else 语句之后

$.ajax({
    ...
    success: function(result) {
            // add a call to the event listener here
    }

【讨论】:

  • 感谢您的帮助 Duncan,它工作的正确位置在 var checkExist = setInterval(function () {}); 下。现在工作。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 2019-11-02
  • 2012-06-30
  • 2016-11-07
  • 2014-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多