以下代码用于在点击标记时显示Google地图上的信息。但由于某些原因,信息窗口的'closeclick'事件无效。早些时候它正在工作,但由于更复杂的需求,如infowindow对象需要重新打开,因为它是在刷新Ajax调用的地图后。因此,地图在一分钟后会保持刷新,并且要求是,如果未在Ajax调用中关闭,则infowindows应保持打开状态。
我们还在地图上绘制多边形线,我们还在它绘制在Google地图上的每个点上打开一个信息窗口,同样的要求也适用于上面的infowindows对象。
$(".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);
}
});
});
提前感谢您提供的任何帮助。
答案 0 :(得分:1)
问题是你只是为了响应ajax请求而创建了infoWindow。但是,当您添加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
}