I have a situation where a default world map is being loaded to a page by the gmap location module. I would like to be able to dynamically set the center and zoom for that map based on some user specific information that I have.
My problem is that I cannot seem to get a javascript reference to the map, so I have no variable to act on using the GoogleAPI setCenter() function. I looked at the post about 'getting a reference to the map object' but it does not seem to work for me because either the getMap() function doesn't work or I don't have the right map ID (for which I tried many possibilities).
Is there any way to get access to a reference to the map object that is on a page, via javascript if possible, so that I can make changes to the map after it is added to the page by the GMap module?
Comments
Comment #1
pbarnett commentedHi LeeJFG!
I'm using
where 'auto1map' is the ID of my map as generated by the GMap module in function gmap_get_auto_mapid().
See http://crdemo.joblessgeek.com/cr_maps_routes for a demo.
Pete.
Comment #2
3rdLOF commentedVery interested in this problem. Same here.
Comment #3
pbarnett commentedHi, kannary!
What's the problem you're having?
I have this working at http://mongolia.charityrallies.org/en/cr_maps_routes and mapping SMS blogs from Siberia at http://mongolia.charityrallies.org/en/twolegsgoodfourwheelsbataar
Pete.
Comment #4
Timo.Kissing commentedComment #5
chrisfromredfinI'm in Drupal 5 here, and I'm theming a view using template.php. In that file, I'm calling drupal_add_js (type = 'theme') to add a js file late in the game. In that JS file, I have pasted the above code, but Firebug tells me "onMapLoad" is not defined. I see it defined (above) with the (function($)) part, so why isn't it seen?
FIGURED IT OUT:
For one, I had to remove the general function($) wrap around the top part. Nextly, the ID if you are using a view to generate your map isn't auto1map or whatever, it's something like view_gmap_NAMEOFVIEW_page or something.
Comment #6
Timo.Kissing commentedAh sorry, I had to adjust the code a bit to put it in here and got the function definition the wrong way round inside the closure (the "general function wrap" you are refering to).
You really should put the closure back around the onMapLoad defintion, otherwise "wait" will be a global variable and might get overwritten by other JS code.
The correct way to define a global function inside a closure is "functionname = function() {}", thus the closure should look like this:
Comment #7
simeIn reference to @pbarnett at #1, I found a variation on that behavior looking at the gmap_geo module.
1. gmap_set_location() was calling gmap_parse_macro()
2. This was calling gmap_get_auto_mapid() and getting "auto1map"
3. gmap_set_location() was ignoring this id and making "loc1".
Comment #8
altrugon commentedI worked with the solution listed at #6 until between my co-worker and I came with this:
Just in case some one else falls around here :)
Comment #9
titouillethanks altrugon :-) work perfectly