2017-04-12 57 views
1

在這個項目中,我有兩種不同的標記,我使用下面的代碼在地圖上顯示。infowindow隱藏在懸停標記

for (i = 0; i < beaches.length; i++) { 
    marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(beaches[i][1], beaches[i][2]), 
     map: mapSingle, 
     icon: icons[beaches[i][3]].icon 
    }); 
    var infowindow = new google.maps.InfoWindow(); 
    var content = beaches[i][0]; 
    google.maps.event.addListener(marker,'mouseover', (function(marker,content,infowindow){ 
     return function() { 
      infowindow.setContent(content); 
      infowindow.open(mapSingle,this); 
     }; 
    })(marker,content,infowindow)); 
    markers.push(marker); 
} 

現在有了新的要求,我必須放置一種可拖動的新型標記。所以,當我做懸停在此拖動的標記我有這樣的代碼

com_current = new google.maps.Marker({ 
    map: mapSingle, 
    draggable: true, 
    icon: com_Image, 
    animation: google.maps.Animation.DROP, 
    position: {lat: parseFloat('20.5937'), lng: parseFloat('78.9629')}, 
}); 
google.maps.event.addListener(com_current, 'mouseover', function() { 
    var info = infowindow.getContent(); 
    infowindow.setContent('show the position of the marker'); 
    infowindow.open(mapSingle, this); 
}); 

現在,我得到的問題是,它的信息窗口變得開放這是確定其他已經打開信息窗口皮的,但一些,反之亦然。我怎樣才能讓其他已經打開的標記顯示,即使我懸停在這個可拖動的標記。

回答

1

因爲您正在使用用於不可拖動標記的相同信息窗口對象。所以請嘗試使用下面的代碼。

com_current = new google.maps.Marker({ 
    map: mapSingle, 
    draggable: true, 
    icon: com_Image, 
    animation: google.maps.Animation.DROP, 
    position: {lat: parseFloat('20.5937'), lng: parseFloat('78.9629')}, 
}); 
infoWindowForDraggableMarker = new google.maps.InfoWindow(); 
google.maps.event.addListener(com_current, 'mouseover', function() { 
    infoWindowForDraggableMarker.setContent('show the position of the marker'); 
    infoWindowForDraggableMarker.open(mapSingle, this); 
}); 

編輯

在您要創建信息窗口代碼中的第一個塊對象下,像權,變更。

var infowindow = new google.maps.InfoWindow(); 
for (i = 0; i < beaches.length; i++) { 
    marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(beaches[i][1], beaches[i][2]), 
     map: mapSingle, 
     icon: icons[beaches[i][3]].icon 
    }); 
    var content = beaches[i][0]; 
    google.maps.event.addListener(marker,'mouseover', (function(marker,content,infowindow){ 
      return function() { 
      infowindow.setContent(content); 
      infowindow.open(mapSingle,this); 
     }; 
    })(marker,content,infowindow)); 
    markers.push(marker); 
} 

並且對於可拖動標記也使用相同的對象,如下所示。

com_current = new google.maps.Marker({ 
    map: mapSingle, 
    draggable: true, 
    icon: com_Image, 
    animation: google.maps.Animation.DROP, 
    position: {lat: parseFloat('20.5937'), lng: parseFloat('78.9629')}, 
}); 
google.maps.event.addListener(com_current, 'mouseover', function() { 
    infowindow.setContent('show the position of the marker'); 
    infowindow.open(mapSingle, this); 
}); 
+0

謝謝@Naveen。如果我將鼠標懸停在一個標記上,我如何隱藏其他打開的infowindow。你能幫忙嗎 – sanin

+0

只需使用一個信息窗口對象。那麼地圖上只會有一個信息窗口。 –

+0

這就是我在我的問題中所做的。但是,它不工作 – sanin