如何编写if语句来检查infowindow是否打开,如果是,则在单击它或其他标记时将其关闭。我的代码在下面!
function add_marker( $marker, map ) {
// var
var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );
// create marker
var marker = new google.maps.Marker({
position : latlng,
map : map
});
// add to array
map.markers.push( marker );
// if marker contains HTML, add it to an infoWindow
if( $marker.html() )
{
// create info window
var infowindow = new google.maps.InfoWindow({
content : $marker.html()
});
// show info window when marker is clicked
google.maps.event.addListener(marker, 'click', function() {
infowindow.open( map, marker );
});
}
}
答案 0 :(得分:0)
根据问题中的代码,它会是这样的,虽然它未经测试:
google.maps.event.addListener(marker, 'click', function() {
var map = infoWindow.getMap();
if (map !== null && typeof map !== "undefined") {
infowindow.close();
} else {
infowindow.open( map, marker );
}
});
答案 1 :(得分:0)
function fun_name()
{
//create the global instance of infoWindow
if(!window.infowindow)
{
window.infowindow=new google.maps.InfoWindow();
}
-------
-------
-------
google.maps.event.addListener(marker, 'click', function() {
infowindow.close();
infowindow.setContent(box_html);
infowindow.open(map,this)
});
}