1

Zooming and positioning the map to the center was not working in (Google map) So we come up with small change in the code by adding time delay to it, then it was started working, may be it will use for someone. (fitbounds method googlemap api v3 for drupal 7 gmap)

jQuery(document).ready(function() {
  // time delay for the maps to initialize.
  setTimeout(function() {

// Loop through all gmap instances.
jQuery(".gmap").each(function() {

  // Get a map reference using the element ID.
  gmapID = jQuery(this).attr("id");
  var gmap = Drupal.gmap.getMap(gmapID).map;

  // Ensure map exists.
  if (gmap) {
    alert(gmap.toSource());
    // Create a new map boundary.
    var bounds = new google.maps.LatLngBounds();
    // Iterate though the current maps marker collection.
    jQuery.each(gmap.markers, function(index) {
      // Find the current markers position (latitude, longitude).
      var data = gmap.markers[index];
      var pos = data.getPosition();

      // Extend the new boundary so the new marker/pin will be in view.
      bounds.extend(new google.maps.LatLng(pos.lat(), pos.lng()));
    });
    // Set the current maps bounding box to our new defined boundary.
    gmap.fitBounds(bounds);
  }
  else {
    // GMap was not initialised in time.
  }

});

 }, 100);
});

This is what the code We added time delay on top of it. https://stackoverflow.com/questions/3520810/zoom-and-center-a-google-map-according-to-its-markers-javascript-api-v3

closed as unclear what you're asking by kiamlaluno Jun 22 '16 at 6:08

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • The question shows the code that is said to be a patch, but there isn't any explicit question. (The question title doesn't count as explicit question.) Showing code and a link to a question on another side doesn't mean asking a question. It's not even clear what is being said. Is the question about reviewing the patch and discovering why it doesn't work? – kiamlaluno Jun 22 '16 at 6:11
0

Instead of a delay, wait for a map loaded event. I could not find a direct one so used a mutation event.

MutationObserver = window.MutationObserver || window.WebKitMutationObserver;

var observer = new MutationObserver(function(mutations, observer) {
  var target_id = mutations[0].target.id;
  var map = Drupal.gmap.getMap(target_id).map;
  if (typeof map != 'undefined') {
    var initialLocation = new google.maps.LatLng(old_lat, old_lon);
    map.setCenter(initialLocation);
    // map.vars.zoom
    map.setZoom(12);
  }
});

jQuery(".gmap").each(function() {
  var gmapID = jQuery(this).attr("id");

  observer.observe($("#" + gmapID)[0], {
    subtree: false,
    attributes: true
  });
});

Subtree = false should mean that once the map is loaded this doesn't get triggered again. Add another test if needed.

Not the answer you're looking for? Browse other questions tagged or ask your own question.