Skip to content Skip to sidebar Skip to footer

Markerclustererplus: Set Icon Color/url Independent Of Size

I'm plotting a few hundred thousand points via MarkerClustererPlus, and I want to set groups of cluster icons (colour) based on some exteral property (not based on number of marker

Solution 1:

So it turns out the trouble was coming from the MarkerClustererPlus lib itself:

656:  function MarkerClusterer(map, opt_markers, opt_options) {
…
665:    opt_options = opt_options || {};

Line 665 creates a reference to the existing object, instead of a new copy. I couldn't use MarkerClusterer.prototype.extend from line 1539 because it does not make a deep copy (and it extends only an object's prototype).

So, I wrote my own deep-copy function (jsfiddle), which I made globally available (rather than add it to the prototypes of both Array and Object):

function deepCopy(obj) {  
  this.cloneArr = function (arr) {
    var newArr = [];
    for ( var i = arr.length-1; i >= 0; i-- ) newArr[i] = this.evalObj( arr[i] );
    return newArr;
  };
  this.cloneObj = function(obj) {
    var newObj = {};
    for ( var prop in obj ) newObj[prop] = this.evalObj( obj[prop] );
    return newObj;
  };
  this.evalObj = function(obj) {
    switch ( typeof obj ) {
      case 'object':
        if ( Array.isArray( obj ) ) returnthis.cloneArr( obj );
        if ( obj instanceof Date === false ) returnthis.cloneObj( obj );
        // pass thru dates, strings, numbers, booleans, and functions
      default: return obj; // primitive
    }
  };
  returnthis.evalObj(obj);
}

I then altered MarkerClustererPlus.js to the following:

656:  function MarkerClusterer(map, opt_markers, opt_optionsG) {
…
665:    var opt_options = deepCopy( opt_optionsG ) || {};

I tested having 5 instances of MarkerClustererPlus (each with 5000 markers, 25000 total), and there was no decernable performance impact compared to having a single MC+ instance.

Screenshot of multiple instances of MarkerClustererPlus

Post a Comment for "Markerclustererplus: Set Icon Color/url Independent Of Size"