2014-02-14 28 views
1

我是一個相對的新手到JavaScript的,所以要溫柔......打開和關閉信息窗口的onclick

誰能幫我的代碼我的事件監聽的內部,使得信息窗口將執行以下操作: 開放,如果它目前已關閉, ,如果當前打開,請關閉。

我已嘗試以下沒有成功...

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

感謝

+0

你真的使用[已棄用(並關閉)Google Maps JavaScript API v2](https://developers.google.com/maps/documentation/javascript/v2/reference)? – geocodezip

回答

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

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; 
     } 
    }) 
0

嘗試第一次近距離INFOW窗口,然後獲得點擊再次打開:

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

欲瞭解更多信息,從這個網址訪問源代碼: