2013-03-12 69 views
0

我使用onclick事件來更改標記圖像,當它被點擊時,顯示它突出顯示。Google Maps API v3:如何在點擊另一個標記時將標記顏色返回原始位置?

當我點擊另一個標記時,我希望這個標記返回到原始圖像,而不是保持突出顯示。

這是代碼;

google.maps.event.addListener(marker, 'click', function() { 
    marker.setIcon("default_h.png") 
    infowindow.open(map, marker); 
    document.getElementById("address_box").value = (title + "\n" + address); 
}); 

我想一種方法是創建一個規則,以便一次只能在地圖上顯示一個高亮顯示的圖像。

回答

1

您必須在某處存儲對單擊標記的引用,然後使用setIcon。

所以,你的代碼看起來依稀像這樣:

var highlighted; 

//create your markers here and assign them event listeners 
var markerObject = new google.maps.Marker({}); 
google.maps.event.addListener(markerObject,'click',function() { 
if(highlighted) { 
    highlighted.setIcon(/* original icon */); 
} 

highlighted = markerObject; 
highlighted.setIcon(/* highlighted icon */); 

}); 
相關問題