Open and Close InfoWindow onclick

空扰寡人 提交于 2019-12-02 10:15:10

You can use something like this:

    infoWindowClosed = true;

    google.maps.event.addListener(marker, 'click', function() {
        if (infoWindowClosed) {
            infowindow.open(map, marker);
            infoWindowClosed = false;
        } else {
            infowindow.close(map, marker)
            infoWindowClosed = true;
        }
    })
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; 
}); 

Source : https://groups.google.com/forum/#!topic/google-maps-js-api-v3/cA2VRg4TO1k

Try first close infow window, and then get click to open again:

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

For more information visit the source code from this url:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!