Skip to content

Instantly share code, notes, and snippets.

@aknosis
Created May 28, 2011 19:32
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aknosis/997144 to your computer and use it in GitHub Desktop.
Save aknosis/997144 to your computer and use it in GitHub Desktop.
Execute callback once when a google map is loaded (google maps api)
//Fill in the blanks :)
var map = new google.maps.Map(),
tileListener = google.maps.event.addListener(map,'tilesloaded',fixMyPageOnce);
function fixMyPageOnce(){
//immediately remove the listener (or this thing fires for every tile that gets loaded, which is a lot when you start to pan)
google.maps.event.removeListener(tileListener);
}
@ismyrnow
Copy link

You can also use the following and avoid having to remove the listener manually:

google.maps.event.addListenerOnce(map, 'tilesloaded', fixMyPageOnce);

function fixMyPageOnce() {
  // do stuff
  // no need to remove the event listener
}

@barrychapman
Copy link

This will call once the map is finished doing its thing.

  google.maps.event.addListenerOnce(map, 'idle', function(){
        // do something only the first time the map is loaded
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment