2012-07-11 56 views
2

我知道這是一個重複的問題,如何在Google map V3中同時打開單個Info窗口?

我在我的Django基於Web的應用程序上使用谷歌地圖v3。我在哪裏使用Markers, Infowindow and polyline。一切正常,除了當我點擊一個標記來顯示內容的信息窗口,前一個打開的信息窗口沒有關閉。

我張貼了我的地圖代碼(只有腳本的一部分或者是有用的):

var marker = add_marker(flightPlanCoordinates[i][0], flightPlanCoordinates[i][1],"Event Detail",myHtml); 

這裏myHtml是包含信息窗口內容的變量。我沒有在這裏定義變量。 SO忽視它。

marker.setMap(map); 
    } 

    var flightPath = new google.maps.Polyline({ 
       path: flightPlanCoordinatesSet, 
       strokeColor: "#FF0000", 
       strokeOpacity: 1.0, 
       strokeWeight: 2 
        }); 
    flightPath.setMap(map); 
} 

function add_marker(lat,lng,title,box_html) { 
var infowindow = new google.maps.InfoWindow({ 
    content: box_html 
}); 

var marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(lat,lng), 
     map: map, 
     title: title 
}); 

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

return marker; 
} 

回答

2

而不是多個infoWindows只使用一個實例。

單擊標記時,先關閉infoWindow,然後設置新內容並打開infoWindow。

function add_marker(lat,lng,title,box_html) 
{ 
    //create the global instance of infoWindow 
    if(!window.infowindow) 
    { 
    window.infowindow=new google.maps.InfoWindow(); 
    } 

    var marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(lat,lng), 
     map: map, 
     title: title 
    }); 

    google.maps.event.addListener(marker, 'click', function() { 
    infowindow.close(); 
    infowindow.setContent(box_html); 
    infowindow.open(map,this) 
    }); 

    return marker; 
} 
+0

感謝您的回覆,我嘗試過使用此選項。但沒有工作。可能是我錯了,你可能會顯示我添加偵聽器方法的僞代碼 – 2012-07-11 08:20:22

+1

修改後的add_marker函數應該可以工作。 – 2012-07-11 08:40:59

+0

@ Dr.Molle如果我創建infoWindow的全局實例,我在'LatLngBounds'中有多個標記,最後一個infowinodow顯示所有的指針 – 2015-07-08 12:01:24

相關問題