2012-02-15 136 views
0

我会尽量做得更精确。我试图打开一个附加到自定义按钮上的标记的'infowindow'。虽然它通过直接点击标记来工作,但我无法将其分配给按钮。该页面目前看起来像这样:map_example谷歌地图:打开infowindow自定义按钮

任何人都可以检查下面的代码并告诉我我做错了什么?

function zagrebControl(controlDiv, map) { 
controlDiv.style.paddingLeft = '0px'; 
controlDiv.style.paddingRight = '6px'; 
controlDiv.style.paddingTop = '0px'; 
controlDiv.style.paddingBottom = '6px'; 
controlDiv.style.marginBottom = '0px'; 
controlDiv.style.width = '80px'; 

var zagrebUI = document.createElement('DIV'); 
zagrebUI.style.backgroundColor = 'white'; 
zagrebUI.style.borderStyle = 'solid'; 
zagrebUI.style.borderWidth = '1px'; 
zagrebUI.style.cursor = 'pointer'; 
zagrebUI.style.textAlign = 'center'; 
zagrebUI.title = 'click to set the map to zagreb'; 
controlDiv.appendChild(zagrebUI); 

var controlText = document.createElement('DIV'); 
controlText.style.fontFamily = 'Arial,sans-serif'; 
controlText.style.fontSize = '12px'; 
controlText.style.paddingLeft = '4px'; 
controlText.style.paddingRight = '4px'; 
controlText.style.paddingTop = '1px'; 
controlText.style.paddingBottom = '0px'; 
controlText.innerHTML = 'ZAGREB'; 
zagrebUI.appendChild(controlText); 

google.maps.event.addDomListener(zagrebUI, 'click', function() { 
map.setCenter(v1); map.setZoom(7) 
}); 
google.maps.event.addListener(zagrebUI, 'click', function() { 
infowindow.setContent('<object width="640" height="360">...</object>'); 
infowindow.open(map, m1) 
}); 

我的标记代码如下所示:

google.maps.event.addListener(m1, 'click', function() { 
infowindow.setContent('<object width="640" height="360">...</object>'); 
infowindow.open(map, m1); 

});

谢谢。

+0

您可以使用infoWindow.setContent(“html content”); – 2012-02-15 17:01:33

回答

0

m1不是全球变数,所以infowindow.open(map, m1)浏览器不知道m1

全球宣布m1

var m1; 
function initialize() { 
... 
m1 = new google.maps.Marker({ 
    position: new google.maps.LatLng(45.81871335, 15.97911), 
    map: map, 
    animation: google.maps.Animation.DROP, 
    title: "ZAGREB" 
}); 
... 
+0

我按照你的建议宣布,但它仍然无法正常工作。 – capitalistica 2012-02-17 09:23:22

+0

你必须为听众点击,第二个不触发使用此google.maps.event.addDomListener(zagrebUI,'点击',函数(){ map.setCenter(v1); map.setZoom(7) infowindow.setContent( '<对象宽度= “640” ....>'); \t infowindow.open(地图,M1) });' – 2012-02-17 09:28:34

+0

抱歉它现在后我已经结合两个事件 – capitalistica 2012-02-17 09:30:36