Get DOM Element of a marker in Google Maps API 3

前端 未结 5 1017
野性不改
野性不改 2020-12-15 04:16

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.

I\'ve

5条回答
  •  攒了一身酷
    2020-12-15 05:04

    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.

提交回复
热议问题