打开并关闭InfoWindow onclick

时间:2014-02-14 07:09:36

标签: javascript google-maps google-maps-api-2

我是javascript的相对新手所以要温柔......

任何人都可以帮我在eventListener中编码,以便infowindow执行以下操作:     如果当前关闭,则打开 如果当前打开则关闭。

我尝试过以下方法但没有成功......

google.maps.event.addListener(marker, 'click', function() {
if(infowindow.closed == true){
infowindow.open(map, marker)
}
else{
infowindow.close(map, marker)
}
})

感谢

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以使用以下内容:

    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 :(得分:0)

首先尝试关闭信息窗口,然后再次点击再次打开:

<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>

有关更多信息,请访问此网址的源代码: