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

pbarnett’s picture

Hi LeeJFG!

I'm using

var map = Drupal.gmap.getMap('auto1map');

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.

3rdLOF’s picture

Very interested in this problem. Same here.

pbarnett’s picture

Hi, 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.

Timo.Kissing’s picture

(function($) {
  // the wait variable is there to prevent endless "looping" in case the map is not on the page
  // the code will search for the map in increasing interval and stop once the interval is larger than a minute
  var wait = 250;
  function onMapLoad(id, func) {
    var m = Drupal.gmap.getMap(id);
    // in case you use a clusterer and need in inside func, add a check to the following condition;
   // of course your code then depends on the settings in the gmap module
    if (m && m.map && m.ready) {
      wait = 250;
      func(m.map);
    } else if (wait < 60001) {
      var funcRef = function() {
        onMapLoad(id, func);
      };
      setTimeout(funcRef, wait);
      wait = wait + 250;
    }
  };
})(jQuery);
jQuery(document).ready(function() {
  onMapLoad('ID-of-your-map-here', function(drupalGMapObject) {
    // code to execute once the map is ready goes here
    // the GMap object is in drupalGMapObject.map
  });
});
chrisfromredfin’s picture

I'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.

// the wait variable is there to prevent endless "looping" in case the map is not on the page
// the code will search for the map in increasing interval and stop once the interval is larger than a minute
var wait = 250;
function onMapLoad(id, func) {
  var m = Drupal.gmap.getMap(id);
  // in case you use a clusterer and need in inside func, add a check to the following condition;
  // of course your code then depends on the settings in the gmap module
  if (m && m.map && m.ready) {
    wait = 250;
    func(m.map);
  } else if (wait < 60001) {
    var funcRef = function() {
      onMapLoad(id, func);
    };
    setTimeout(funcRef, wait);
    wait = wait + 250;
  }
};

jQuery(document).ready(function() {
  onMapLoad('view_gmap_wio_map_view_page', function(drupalGMapObject) {
    // code to execute once the map is ready goes here
    // the GMap object is in drupalGMapObject.map
    alert('jquery');
  });
});
Timo.Kissing’s picture

Ah 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:

(function($) {
  // the wait variable is there to prevent endless "looping" in case the map is not on the page
  // the code will search for the map in increasing interval and stop once the interval is larger than a minute
  var wait = 250;
  onMapLoad = function (id, func) {
    var m = Drupal.gmap.getMap(id);
    // in case you use a clusterer and need in inside func, add a check to the following condition;
   // of course your code then depends on the settings in the gmap module
    if (m && m.map && m.ready) {
      wait = 250;
      func(m.map);
    } else if (wait < 60001) {
      var funcRef = function() {
        onMapLoad(id, func);
      };
      setTimeout(funcRef, wait);
      wait = wait + 250;
    }
  };
})(jQuery);
sime’s picture

In 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".

altrugon’s picture

I worked with the solution listed at #6 until between my co-worker and I came with this:

Drupal.gmap.addHandler('gmap', function (elem) {
  var obj = this;
  
  /* Add new handlers only to the desired map(s) */
  if (obj.vars.id == "mymapid") {
    
    // At this point your map is ready but not your icons
    obj.bind('ready', function () {
      // Your map is located in obj.map
      console.log(obj.map);
    });
    
    // At this point your icons are ready, useful if you plan to use
    // things like "Drupal.gmap.sequences"
    obj.bind('iconsready', function () {
      // Your map is located in obj.map
      console.log(obj.map);
    });
  }
  
});

Just in case some one else falls around here :)

titouille’s picture

thanks altrugon :-) work perfectly