English 中文(简体)
Get DOM 页 次
原标题:Get DOM Element of a marker in Google Maps API 3

我试图找到一种办法,使管理局的一个标识。 谷歌似乎利用不同的要素显示标识和处理活动。

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.

其次,如果在标注上附上一个点击事件,那么APIC就把DOM要素作为对你所指明的职能的论据。 在“火药”中,我发现两者之间有任何关系。

我想要完成的是操纵OM(OM)分子,然后把更多的信息提供给它,只是一个具有背景的干.分子。

我已在第2版中采用名称财产(无文件记载)来做这项工作,该名称财产产生红利。

是否有任何人的想法?

最佳回答

我发现,工作确实很差。 人们可以使用所有权属性来通过补贴财产。

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 ve found this method to be useful, although it might not be suitable for all environments. When setting the marker image, add a unique anchor to the URL. For example:

// Create the icon
var icon = new google.maps.MarkerImage(
    "data/ui/marker.png",
    new google.maps.Size(64, 64),
    new google.maps.Point(0,0),
    new google.maps.Point(48, 32)
);

// Determine a unique id somehow, perhaps from your data
var markerId = Math.floor(Math.random() * 1000000);
icon.url += "#" + markerId;

// Set up options for the marker
var marker = new google.maps.Marker({
    map: map,
    optimized: false,
    icon: icon,
    id: markerId,
    uniqueSrc: icon.url
});

现在,你们有一个独特的选择,即:

$("img[src= data/ui/marker.png#619299 ]")

或如果您有标识:

$("img[src= " + marker.uniqueSrc + " ]")

I was looking for the DOM element too in order to implement a custom tooltip. After a while digging into google overlays, custom libraries and so on, i ve ended up with the following solution based on fredrik s title approach (using jQuery) :

google.maps.event.addListener(marker,  mouseover , function() {

    if (!this.hovercardInitialized) {

        var markerInDOM = $( div[title="  + this.title +  "] ).get(0);

        // do whatever you want with the DOM element

        this.hovercardInitialized = true;
    }

});

希望有助于人们。

  1. Customize overlays (by implements onAdd, draw, remove) while drag it has some issues.
  2. Maybe you can get the marker dom element by the class name gmnoprint and the index it has been appended in.

我已经核实,Gogle地图标点点击活动有MosueEvent财产,而模拟活动的目标就是标识的主角。

它为我工作,直到发现财产名称从拖拉改到 r为止。 我不知道为什么,而且我无法找到解释,因为在洞 go图中,这种解释是一纸空洞,但我已围绕这一点做了工作。

我检查了点击事件物体的特性,这是MouseEvent的事例,我把目标作为标识。

marker.addListener( click , (e) => {

      let markerElement;

   // with underscore
    _.toArray(markerClickEvent).some(item => {
        if (item instanceof MouseEvent) {
            markerElement = item.target;
            return true;
        }
    });
  // or maybe with for loop
for (let key in markerClickEvent) {
        if (markerClickEvent.hasOwnProperty(key) && markerClickEvent[key] instanceof MouseEvent) {
            markerElement = markerClickEvent[key].target;
            break;
        }
    }
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签