Laxmidi
Laxmidi

Reputation: 2684

How can I reference a google map in another script?

How can I reference a google map in another script? On my WordPress page, I load javascript (a) that builds my map, and jQuery script (b). I need to figure out some way passing a reference to the map to script (b). The problem is that the map is created inside a function in script (a).

In script (a), I've got:

function map_maker_js( args ) {
var map = new google.maps.Map(document.getElementById( args['id'] ), myOptions);

//code for building map continues
}

In script (b):

jQuery.noConflict();

jQuery(document).ready(function($) { 

//say I need the map's bounds
//how can I access map, in order for this to work: 
map.getBounds();

}

I saw this stackoverflow solution, but I couldn't get it to work.

Upvotes: 0

Views: 523

Answers (1)

Ilia Choly
Ilia Choly

Reputation: 18557

stick it in the global namespace.

function map_maker_js( args ) {
     window.map = new google.maps.Map(
          document.getElementById( args['id'] ), myOptions);

     //code for building map continues
}

jquery

jQuery.noConflict();

jQuery(document).ready(function($) { 
    window.map.getBounds();
}

but make sure map_maker_js finishes running first.

Upvotes: 1

Related Questions