quanduyle / google-maps-utility-library-v3

Automatically exported from code.google.com/p/google-maps-utility-library-v3
0 stars 0 forks source link

MarkerClusterer clusterclick firing twice #132

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
  mc.addMarkers(clusters);
  google.maps.event.addListener(mc, 'clusterclick', function(cluster){

      google.maps.event.removeListener(idle);

      var content = '';

      clusterMarkers = cluster.getMarkers();

      map.setCenter(clusterMarkers[0].getPosition());
      var info = new google.maps.MVCObject;
          info.set('position', cluster.center_);

      infoWindow.setContent(content);
      infoWindow.open(map, info);

      setTimeout(function(){
                  idle = google.maps.event.addListenerOnce(map, 'idle', searchLocationsNear);},2000);
});

Expected result:
On clusterclick, Infowindow opens then after 2 second, the idle listener is 
readded.

Actual result:
On clusterclick, the above code is fired twice, then after 2 second, the idle 
listener is readded twice.

Version: 2.0.5

Browser / Operating System:
Firefox/Mac OSX 10.7

Additional comments: In my debugging, I've found that the outer function in 
which the above code is housed is not firing more than once, but for some 
reason when I click on a map cluster, the clusterclick function fires twice 
immediately despite only clicking once.

*********************************************************
Tip: Star this issue (next to title) to receive notifications of status
changes against this issue, also used as a gauge for how many people are
interested in seeing it resolved.
*********************************************************

Original issue reported on code.google.com by nmccrac...@gmail.com on 27 Sep 2011 at 1:04

GoogleCodeExporter commented 8 years ago
Your code fragment has some issues.

-- There is no var for the idle variable

-- cluster.center_ should not be used, use cluster.getCenter() instead

-- there is no var for infoWindow.

-- You pass "clusters" to the addMarkers function but addMarkers takes an array 
of markers (I'm not sure what "clusters" contains because it is not defined 
anywhere.)

A live example illustrating the problem would be useful.

I just checked the events example provided with MarkerClustererPlus and it 
reports that only one event is fired after a cluster is clicked.

Original comment by garylitt...@gmail.com on 27 Sep 2011 at 4:01

GoogleCodeExporter commented 8 years ago
Thanks for getting back to me.

The idle, infoWindow, and clusters array are all global variables, which is why 
they're not defined here.

The infoWindow is appearing appropriately, but the onClick for the clusters 
does something funny. The first time it runs, it appears to fire twice, after 
that, it appears to go up in numbers. If you click it again, it fires 3 times, 
then 4 and so on.

I'm not sure if that helps, I don't really have a live version I can easily 
link to.

Original comment by nmccrac...@gmail.com on 27 Sep 2011 at 6:55

GoogleCodeExporter commented 8 years ago
Since you are providing your own click handler, you should turn off the default 
click handler. Do this by setting the zoomOnClick property to false in the 
MarkerClustererOptions object you pass when creating the MarkerClusterer.

Original comment by garylitt...@gmail.com on 27 Sep 2011 at 8:51

GoogleCodeExporter commented 8 years ago
Thanks, I've done that. Is there anything I need to adjust in the javascript 
file that could be the culprit?

Original comment by nmccrac...@gmail.com on 28 Sep 2011 at 12:26

GoogleCodeExporter commented 8 years ago
Until you post a complete demo, I can't tell what's causing the problem. 

Original comment by garylitt...@gmail.com on 28 Sep 2011 at 5:13

GoogleCodeExporter commented 8 years ago
Thanks for getting back to me.
I've written up a short demo which can be found here:
http://exents.com/marker-simple.html

I have isolated that the problem starts when the bounds are changed in any way. 
So if you hit the cluster before changing the bounds, you'll get the 
CLUSTERFIRE alert, which simply tells you the cluster was fired, then a few 
seconds later, you'll get the Idle Has Been Readded alert, which lets you know 
that the idle listener that usually exists has come back.

If you change your bounds, you'll get all this twice. Change it again, you get 
it 4 times, change it again, you get 6 and so on.

Maybe this has something to do with my idle listeners being in place?
I'm honestly not sure.

Original comment by nmccrac...@gmail.com on 28 Sep 2011 at 3:53

GoogleCodeExporter commented 8 years ago
The problem is that your idle handler calls "clusterer" which keeps pushing the 
same markers to the end of the clusters array... and then it installs another 
click handler. So every time you pan the map, another click handler is 
installed. In other words, your code is buggy.

Original comment by garylitt...@gmail.com on 28 Sep 2011 at 4:16

GoogleCodeExporter commented 8 years ago
Would something like:

mc.clearMarkers(); and setting the idle listener to addListenerOnce take care 
of that problem.

In my original code, I have a recurring clearMarkers() program that is supposed 
to clear the markers from the Clusterer everytime it's run. It additionally 
splices the cluster array until everything is removed from it.

Will the listener be added again in this case if its set to addListenerOnce?

Original comment by nmccrac...@gmail.com on 28 Sep 2011 at 4:31

GoogleCodeExporter commented 8 years ago
The way your code is written now, the first idle event after you click a marker 
causes another click handler to be installed. That's not what you want. 

Original comment by garylitt...@gmail.com on 28 Sep 2011 at 4:45

GoogleCodeExporter commented 8 years ago
You're right, I found my solution. The key was taking it out of the function it 
was in. Putting the listener somewhere that the idle didn't call it took care 
of the problem.
Thanks for your help and patience!

Original comment by nmccrac...@gmail.com on 28 Sep 2011 at 5:02