简体   繁体   English

加载谷歌地图控件时是否有事件?

[英]Is there an event for when google maps controls are loaded?

I know of tilesloaded , but the controls seem to load after that event.我知道tilesloaded ,但控件似乎在该事件之后加载。

I basically want to be able to grab the controls via jQuery, but can't find the correct even to listen for.我基本上希望能够通过 jQuery 获取控件,但无法找到正确的监听。

I just dealt with it too.我也刚处理过。 There is no event like that (idle and tilesloaded triggers before the controls are visible).没有这样的事件(在控件可见之前空闲和tileloaded 触发器)。

So, basicaly add "special-control" class to your controls, then just check in interval if they are loaded or not... when jquery returns positive length, then the controls are fully loaded.因此,基本上将“特殊控制”类添加到您的控件中,然后只需检查间隔是否加载它们......当 jquery 返回正长度时,则控件已完全加载。

function GetTestControl()
{
    var control = document.createElement('div');
    control.className = 'special-control'; // THERE IS THE SPECIAL CLASS
    control.style.margin = '0 0 10px 10px';

    var btn = document.createElement('div');
    btn.innerHTML = '<i  class="fa fa-globe"></i>';
    control.appendChild(btn);

    btn.addEventListener('click', function()
    {
        alert("test");
    });

    return control;
}

google.maps.event.addListener(map, 'tilesloaded', function()
{
    var sinterval = setInterval(function()
    {
        if($('.special-control').length > 0)
        {
            // controls loaded, do whatever you want
            // + stop interval so it doesn't ruin your memory
            clearInterval(sinterval);
        }
    }, 500); // should work perfectly, but you can decrease
});

map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(GetTestControl());

And that's it.就是这样。

您可以通过在 Chrome 中按 F12 并单击时间轴选项卡来查看正在触发的事件。

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

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