Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 the correct even to listen for.

like image 222
Derek Reynolds Avatar asked Aug 04 '11 17:08

Derek Reynolds


1 Answers

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.

like image 67
Tomáš Petr Avatar answered Oct 26 '22 23:10

Tomáš Petr