我正在尝试编写一个干净的 GoogleMap.js 脚本。我创建了一个包含 gMarker 和 gInfoWindow 的 js 类,我想在其原型(共享)中设置一个“openedInfoWindow”属性,因此我可以在用户单击特定 gMarker 时关闭它并更改它,而无需将其声明为全局。
function gMarkerWInfo(gMarker,gInfoWindow){
if(!gMarker || !gInfoWindow)
return null;
this.Marker = gMarker;
this.InfoWindow = gInfoWindow;
}
gMarkerWInfo.prototype.openedInfoWindow = null;
gMarkerWInfo.prototype.openInfoWindow = function(){
if(this.openedInfoWindow){
alert(this.openedInfoWindow.getContent());
//openedInfoWindow.close();
}
this.InfoWindow.open(this.Marker.getMap(),this.Marker);
this.openedInfoWindow = this.InfoWindow;
}
“警报”用于调试目的,每次我单击它时,它都会向我显示“链接”到我刚刚单击的 gMarker 的 InfoWindow 的内容。所以“openedInfoWindow”不能像我希望的那样工作。谁能帮我?
PS。这是我用来在“GoogleMap”类中创建 gMarkerWInfo 的函数:
this.createMarkerWInfo = function(LatLng,Name,HTML_Infos){
var gMarker = new google.maps.Marker({ position: LatLng,
animation: this.MarkerAnimation,
map: priv_Map,
title: Name
});
var gInfoWindow = new google.maps.InfoWindow({content:HTML_Infos});
var gMarkerWInfoWindow = new gMarkerWInfo(gMarker,gInfoWindow);
google.maps.event.addListener(gMarker,'click', function() {gMarkerWInfoWindow.openInfoWindow();});
return gMarkerWInfoWindow;
}