开发者

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

开发者 https://www.devze.com 2023-03-25 03:15 出处:网络
I know of tilesloaded, but the controls seem to load after that event. I basically want to be able to grab the controls via jQuery, but can\'t find th开发者_如何学Ce correct even to listen for.I just

I know of tilesloaded, but the controls seem to load after that event.

I basically want to be able to grab the controls via jQuery, but can't find th开发者_如何学Ce correct even to listen for.


I just dealt with it too. There is no event like that (idle and tilesloaded triggers before the controls are visible).

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.

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.


You can view which events are firing by hitting F12 in Chrome and clicking the Timeline tab.

0

精彩评论

暂无评论...
验证码 换一张
取 消