【问题标题】:Open and Close InfoWindow onclick打开和关闭信息窗口 onclick
【发布时间】:2014-02-14 07:09:35
【问题描述】:
谁能帮我在 eventListener 中编写代码,以便信息窗口执行以下操作:
如果当前关闭,则打开,
如果当前打开,则关闭。
我尝试了以下没有成功...
google.maps.event.addListener(marker, 'click', function() {
if(infowindow.closed == true){
infowindow.open(map, marker)
}
else{
infowindow.close(map, marker)
}
})
【问题讨论】:
标签:
javascript
google-maps
google-maps-api-2
【解决方案1】:
你可以这样使用:
infoWindowClosed = true;
google.maps.event.addListener(marker, 'click', function() {
if (infoWindowClosed) {
infowindow.open(map, marker);
infoWindowClosed = false;
} else {
infowindow.close(map, marker)
infoWindowClosed = true;
}
})
【解决方案2】:
var currentInfoWindow = null;
then on every marker click event I do something like this:
var infowindow = new google.maps.InfoWindow({
content: "your content here"
});
google.maps.event.addListener(marker, 'click', function() {
if (currentInfoWindow != null) {
currentInfoWindow.close();
}
infowindow.open(map, marker);
currentInfoWindow = infowindow;
});
来源:https://groups.google.com/forum/#!topic/google-maps-js-api-v3/cA2VRg4TO1k
【解决方案3】:
尝试先关闭信息窗口,然后再点击打开:
<script>
var onMarkerClick = function() {
var marker = this;
var latLng = marker.getPosition();
infoWindow.setContent('<h3>Marker position is:</h3>' +
latLng.lat() + ', ' + latLng.lng());
infoWindow.open(map, marker);
};
google.maps.event.addListener(map, 'click', function() {
infoWindow.close();
});
var marker1 = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(37.789879, -122.390442)
});
google.maps.event.addListener(marker1, 'click', onMarkerClick);
</script>
欲了解更多信息,请访问此网址的源代码: