2016-04-15 105 views
0

我正在使用標記infoWindow來顯示信息。它會正確打開,並在地圖上某處點擊時關閉。Infowindow on marker不關閉(Google地圖)

initMap(){ 
................. 
    map.addListener('click', function() { 
     infowindow.close(); 
    }); 
} 



var infowindow; 
..... 
............. 
function markerPushWithInfoWindow(marker) { 
    markers.push(marker); 
    infowindow = new google.maps.InfoWindow(); 
    marker.addListener('click', function() { 
     if (infowindow) { 
      infowindow.close(); 
     } 
     infowindow.setContent(this.info); 
     infowindow.open(map, marker); 
    }); 
} 

markerPushWithInfoWindow is called()when marker is drawn during animation。在動畫運行時,infoWindow不會關閉(當在標記之外單擊時,即在地圖上)時,它僅在動畫暫停時關閉。

動畫:我們從數據庫(特定日期)獲取位置(緯度/經度)數據列表並通過汽車動畫(重播功能)。

+0

什麼動畫?請提供一個[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)來說明問題。 – MrUpsidown

+0

如果你正在爲每個標記創建一個InfoWindow對象......那可能不是要走的路。 – MrUpsidown

+0

@MrUpsidown不,我們正在爲每個對象創建單獨的對象。 –

回答

0

下面是如何在共享一個共同infowindow的循環中創建多個標記的示例。請注意標記事件偵聽器上使用閉包。

function initialize() { 

    var mapOptions = { 
    zoom: 5, 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    center: new google.maps.LatLng(1, 1) 
    }; 

    var locations = [ 
    [new google.maps.LatLng(0, 0), 'Marker 1', 'Infowindow content for Marker 1'], 
    [new google.maps.LatLng(0, 1), 'Marker 2', 'Infowindow content for Marker 2'], 
    [new google.maps.LatLng(0, 2), 'Marker 3', 'Infowindow content for Marker 3'], 
    [new google.maps.LatLng(2, 0), 'Marker 4', 'Infowindow content for Marker 4'], 
    [new google.maps.LatLng(2, 1), 'Marker 5', 'Infowindow content for Marker 5'], 
    [new google.maps.LatLng(2, 2), 'Marker 6', 'Infowindow content for Marker 6'] 
    ]; 

    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
    var infowindow = new google.maps.InfoWindow(); 

    google.maps.event.addListener(map, 'click', function() { 
    infowindow.close(); 
    }); 

    for (var i = 0; i < locations.length; i++) { 

    var marker = new google.maps.Marker({ 
     position: locations[i][0], 
     map: map, 
     title: locations[i][1] 
    }); 

    google.maps.event.addListener(marker, 'click', (function(marker, i) { 

     return function() { 
     infowindow.setContent(locations[i][2]); 
     infowindow.open(map, marker); 
     } 

    })(marker, i)); 
    } 
} 

initialize(); 

JSFiddle demo

編輯:

例不使用封閉

JSFiddle demo

相關問題