谷歌地图:使用自定义按钮打开infowindow

时间:2012-02-15 16:23:44

标签: javascript button google-maps-api-3 infowindow eventtrigger

我会尽量更精确。我试图打开一个从自定义按钮附加到标记的“infowindow”。虽然它通过直接单击标记工作,但我没有将它分配给按钮。该页面目前看起来像这样:map_example

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

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);

});

谢谢。

1 个答案:

答案 0 :(得分: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"
});
...