Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not fire 'zoom_changed' event when calling fitBounds function on map

Is there a way to prevent the zoom_change event from being triggered if it occurs due to fitBounds() ?

I am having an issue where I need to do a search on the server from client when there is a zoom change to map but every time I call fitBounds() it causes zoom_change to trigger which causes the client to do another search on the server. I am only interested in zoom_change done by users and not programmatically using fitBounds.

like image 569
Encore PTL Avatar asked Oct 25 '12 22:10

Encore PTL


4 Answers

When you do a fitBounds in your program, set a global flag. When the zoom_changed event fires, if the flag is set, clear it, otherwise send your request off to the server.

like image 132
geocodezip Avatar answered Oct 24 '22 02:10

geocodezip


After many frustrating hours, here is my solution:

  var tiles_listener = google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
    var zoom_listener = google.maps.event.addListener(map, 'zoom_changed', function() {
      reloadMarkers();
    });
  });

Note that I am calling addListenerOnce for tilesloaded to ensure that the zoome_changed listener is only added once, after the first fitBounds completes.

like image 29
Abram Avatar answered Oct 24 '22 01:10

Abram


It's an old question, but it may be useful to others. I had the same problem, zoom_changed been triggered every time I called fitBounds() when adding several markers to a map. What I did was to add the listener to the map_changed event after the map was completely loaded, like this:

google.maps.event.addListener(map, 'tilesloaded', function() {
    google.maps.event.addListener(map, 'zoom_changed', function() {
like image 4
kito Avatar answered Oct 24 '22 01:10

kito


There is another way to do that by removing that that event. I think it would be much easier and cleaner than adding a global variable.

 vm.map.events.bounds_changed = function() {
   google.map.event.removeListener(zoom_changed);
   }

Another way (forgotten to add that):

     vm.map.events.bounds_changed = function() {
google.map.event.addEventListener(zoom_changed,function(mapper,eventName,args){});
           }
like image 1
abby37 Avatar answered Oct 24 '22 02:10

abby37