GooglemapV3 信息窗口关闭点击事件没有起火

GooglemapV3 infowindow closeclick event is not getting fire

下面的代码用于在点击标记时在 Google 地图上显示信息窗口。但由于某些原因 'closeclick' 信息 windows 事件无法正常工作。虽然早些时候它可以工作,但由于更复杂的要求,如 infowindow 对象需要在 Ajax 调用刷新地图后重新打开。所以地图在一分钟后保持刷新,要求是,如果 infowindows 在 Ajax 调用时未关闭,则应保持打开状态。

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

        $(".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);

            }

        });


});

在此先感谢您提供的任何帮助。

问题是您只是创建信息窗口以响应 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
    }