简体   繁体   English

InfoWindows中的函数调用

[英]Function calls within InfoWindows

Often I have maps with a whole lot of markers (let's say representing shops). 我经常在地图上标记很多标记(例如代表商店)。 I have infowindows that display basic info on the shop represented by the marker, then, on the infoWindow HTML I like to put a button that, for example, says "Details". 我有一些信息窗口,这些窗口显示标记所代表的商店的基本信息,然后在infoWindow HTML上放置一个按钮,例如说“ Details”。 The html of the infowindow is simple.. the html of the info window would include an 信息窗口的html很简单。信息窗口的html将包含一个

input type="button" value="Show More" onclick="showMore(' + shopId + ');

The relevant shopId would be obviously different for every marker... 每个标记的相关shopId显然是不同的...

The problem is that the showMore function has to be declared as a global function to the javascript otherwise the infowindow doesn't find it. 问题是必须将showMore函数声明为javascript的全局函数,否则信息窗口将找不到它。

Lets say that all the code (generating map, placing markers, declaring infowindows, etc) is in a function called function showShops() {} , and the showMore(id) function is inside the showShops() function, HOw can I tell the "onclick" event to call the showMore() function inside the showShops function? 可以说,所有代码(生成地图,放置标记,声明信息窗口等)都在一个名为function showShops() {} ,而showMore(id)函数在showShops()函数内部,我该如何告诉“ onclick”事件在showShops函数内部调用showMore()函数?

Just to check my code, I've changed it to onclick="alert(' + shopId + ') .. and I correctly get an alert with the relevant shop id.. 只是为了检查我的代码,我将其更改为onclick="alert(' + shopId + ') ..并且我正确地收到了带有相关商店ID的警报。

The content of a infowindow may either be a string or a node . 信息窗口的内容可以是字符串或节点

To achieve it you must use a node that will be created inside the scope of showShops() 为此,必须使用将在showShops()范围内创建的节点。

Sample-creation of such a node: 创建这样的节点的样本:

var content = document.createElement('div');
content.innerHTML = 'some text<br/>';

var button = content.appendChild(document.createElement('input'));
button.type = 'button';
button.value = 'click me!';

google.maps.event.addDomListener(button,'click', function(){showMore(id)});

Simple Demo: http://jsfiddle.net/doktormolle/8ZsSp/ 简单演示: http : //jsfiddle.net/doktormolle/8ZsSp/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM