waqasriaz / google-maps-utility-library-v3

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

Bad updateIcon when we try to filter the markers into the MarkerClusterer #55

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

I used your development version of the MarkerClusterer. I try to filter markers 
on the fly. So, in a first time, I try to change the visibility of the marker. 
However, MarkerClusterer doesn't preserve and use this visibility. So, I try 
another solution.

I found a topic 
(http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/83f28
a21e7fe8578/27370bfa62992f18?pli=1) where it suggests to create another array. 
When we want to filter, we clear the manager, search markers and inject these. 
So, I do the following stuff:

MarkerClusterer.prototype.______pushMarkerTo_ = 
MarkerClusterer.prototype.pushMarkerTo_; // Preserve the old one
MarkerClusterer.prototype.pushMarkerTo_ = function(marker){
    if(this.allMarkers == undefined){
        this.allMarkers = [];
    }

    this.allMarkers.push(marker);
    this.______pushMarkerTo_(marker);
};

MarkerClusterer.prototype.______clearMarkers = 
MarkerClusterer.prototype.clearMarkers; // Preserve the old one
MarkerClusterer.prototype.clearMarkers = function(){
    this.______clearMarkers();
    this.allMarkers = [];
};

MarkerClusterer.prototype.refresh = function(filters) {
    this.______clearMarkers();

    for (var i = 0, marker, visible; marker = this.allMarkers[i]; i++) {
        if(/** My condition */){
            this.markers_.push(marker);
        }
    }

    this.redraw();
};

However, I see that cluster with only one marker will be hidden. I found the 
problem into the "updateIcon" form the Cluster class. I replace the method with 
the following statement:

Cluster.prototype.updateIcon = function() {
  var zoom = this.map_.getZoom();
  var mz = this.markerClusterer_.getMaxZoom();

  if (zoom > mz) {
    // The zoom is greater than our max zoom so show all the markers in cluster.
    for (var i = 0, marker; marker = this.markers_[i]; i++) {
      marker.setMap(this.map_);
      marker.setVisible(true);
    }
    return;
  }

  if (this.markers_.length < 2) {
    // We have 0 or 1 markers so hide the icon.
    this.clusterIcon_.hide();

// --------------------------------- Fix
    if(this.markers_.length == 1){
        this.markers_[0].setMap(this.map_);
        this.markers_[0].setVisible(true);
    }
// --------------------------------- Fix
    return;
  }

  var numStyles = this.markerClusterer_.getStyles().length;
  var sums = this.markerClusterer_.getCalculator()(this.markers_, numStyles);
  this.clusterIcon_.setCenter(this.center_);
  this.clusterIcon_.setSums(sums);
  this.clusterIcon_.show();
};

This is a good fix or I missed something in my refresh method ?

Thank you a lot for your great API

Julien Roche

Original issue reported on code.google.com by roche....@gmail.com on 26 Nov 2010 at 2:52

GoogleCodeExporter commented 8 years ago
Instead of pasting code, do you happen to have a link? Its easier to parse than 
what you have here. Thanks!

Original comment by lu...@google.com on 5 Dec 2010 at 10:58

GoogleCodeExporter commented 8 years ago
Hi,

I can't because this is an internal project for the intranet. So, I can't give 
you a link for my purpose :/

Sorry

Original comment by roche....@gmail.com on 6 Dec 2010 at 8:11