1

I'm using google maps api v3. I need to catch all idle events on map but I don't want to see them when I change bounds or zoom via code. My problem exists when I use fitBounds method. I'm using a flag to decide whether an event created because of me or the user. When I used fitBounds, if the bounds are changed, it's fired but sometimes the bounds aren't changed and the event is not fired, so the flag works wrong. It causes the script to ignore user's next event. Here is my code:

var flag = false;

google.maps.event.addListener(map, 'idle', function() {
    if(flag) {
        // ignore this event
    } else {
        // do some work
    }
    flag = false;
});

function fitBounds() {
    var bounds = new google.maps.LatLngBounds();
    for(var i=0; i<markers.length; i++) {
        var location = markers[i].getPosition();
        bounds.extend(location);
    }

    // I need something like whether bounds will change or not after fitBounds?
    flag = true;
    map.fitBounds(bounds);
}
sedran
  • 3,498
  • 3
  • 23
  • 39

2 Answers2

1

You are resetting the flag to false in the wrong place. Try this instead:

google.maps.event.addListener(map, 'idle', function() {
   if(flag) {
         // ignore this event
          flag = false; // reset to false here
          return;
   } 
   else {
     // do some work
   }
});

function fitBounds() {
        flag = true;
    // rest of the function....
}

Aside from that, it might be better to attach the listener to the bounds_changed instead of idle event.

Marcelo
  • 9,387
  • 3
  • 35
  • 40
  • 1
    there is no difference between your code and mine. you say if it's true, make it false, if it's false, don't touch it. So flag is always false after the function, which is the same as mine. – sedran Aug 13 '12 at 09:01
  • That's why I said that it might be better to attach the listener to the bounds_changed instead of idle event. In that case, if the user moved the map, the event fires with the flag = false and you "do some work", while if you .fitBounds() programatically you have flag = true and you ignore the event. Similar to this other question: http://stackoverflow.com/questions/11790905/catch-bounds-changed-event-when-user-moves-the-map-but-not-the-setcenter-method – Marcelo Aug 13 '12 at 09:11
  • When I use bounds_changed, it's fired more than once while fitBounds :( I can't handle it with a boolean and I don't know how much it will be fired. – sedran Aug 13 '12 at 09:34
  • It should only fire once, but it does so asynchronously. Can you offer a demonstrator page in which it fires more than once? – Marcelo Aug 13 '12 at 09:41
  • Sorry, I can't show a page because I'm trying to develop a complicated Windows 8 Metro Application with Javascript, so there's no page. I can show a console log: http://tinypaste.com/badb179a As you see in the link, after I called fitBounds(), bounds_changed event sometimes comes twice, sometimes once, sometimes never. – sedran Aug 13 '12 at 10:04
0

I am not sure, if it would work as api sometimes takes controls (on the sides of the map) into account when calculating bounds and sometimes not, but I think it's worth a shot:

if (!map.getBounds().contains(bounds.getNorthEast()) ||
    !map.getBounds().contains(bounds.getSouthWest()))
{
    flag = true;
    map.fitBounds(bounds);
}
slawekwin
  • 6,270
  • 1
  • 44
  • 57
  • map's bounds can contain that bounds, but the new bounds may require a smaller area, so that the map can zoom. – sedran Aug 13 '12 at 08:22