在Google Maps API 3中获取标记的DOM元素 [英] Get DOM Element of a marker in Google Maps API 3
问题描述
我正在尝试找到一种获取标记的DOM元素的方法。似乎Google使用不同的元素来显示标记和处理事件。
I'm trying to find a way to get the DOM element of a marker. It seems that Google uses different elements for showing a marker and one of handling the event.
到目前为止,我已经弄清楚了两件事情。
有一个生成的变量叫做 fb
,它将DOM元素保存在一个标记对象上,但是我认为下一次Google更新API时,它将被命名为不同的东西。所以不要去。
I've figured out 2 things so far.
There's a generated variable called fb
that holds the DOM element on a marker object, but I think the next time Google updates the API, it will be named something different. So no go.
其次,如果将点击事件附加到标记,API会将事件DOM元素作为参数发送给您指定的功能。当在Firebug中查看它时,我找不到两者之间的任何关系。
Second, if one attach a click event to a marker the API send the event DOM element as arguments to what ever function you have specified. While looking through it in Firebug I can't find any relations between the two.
我想要完成的是操纵DOM元素()并提供它更多信息,然后只是一个'div'元素与背景。
What I'm trying to accomplish is to manipulate the DOM element() and feed it more information then just a 'div' element with a background.
我已经在版本2中使用名称属性(未记录)在div上生成一个id元素
I've done this in version 2 using a name property(undocumented) that generates an id on the div element.
有没有人有想法?
推荐答案
一个真的很糟糕的解决方法。可以使用title属性传递id属性。
I found a really really bad workaround. One can use the title attribute to pass a id property.
fixMarkerId = function () {
$('div[title^="mtg_"]').each(function (index, elem) {
el = $(elem);
el.attr('id', el.attr('title'));
el.removeAttr('title');
});
},
tryAgainFixMarkerId = function () {
if ($('div[title^="mtg_"]').length) {
fixMarkerId();
} else {
setTimeout(function () {
tryAgainFixMarkerId();
}, 100);
};
}
if ($('div[title^="mtg_"]').length) {
fixMarkerId();
} else {
setTimeout(function () {
tryAgainFixMarkerId();
}, 100);
};
我绝对不会为任何生产环境推荐此解决方案。但现在我用它,以便我可以继续发展。但仍然寻找更好的解决方案。
I would strongly not recommend this solution for any production environment. But for now I use it so that I can keep developing. But still looking for a better solution.
这篇关于在Google Maps API 3中获取标记的DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!