MarkerClustererPlus: set icon color/url independent of size

寵の児 提交于 2019-12-03 08:06:25

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 ) ) return this.cloneArr( obj );
        if ( obj instanceof Date === false ) return this.cloneObj( obj );
        // pass thru dates, strings, numbers, booleans, and functions
      default: return obj; // primitive
    }
  };
  return this.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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!