[转][已解决]百度地图MarkerClusterer,Marker的Label丢失的问题

描述下症状   

当将自定义的Marker(含有Label)通过MarkerClusterer 管理的时候,当地图发生任何移动、缩放 的时候,Marker 的Label 就会自动消失

关于解决方案

网上我找到了这个解决方案Nautilus 同学的一个解决方案http://blog.sina.com.cn/s/blog_484d87770101empl.html  

屏蔽了Cluster.prototype.remove 中的

this._markers.getMap() && this._map.removeOverlay(this._markers); 这句话  

但是其实这句话的调用是 _redraw 当中的_clearLastClusters 里清除Cluster用的,其他操作的时候并没有走这个,所以就导致了他说的“不完美”解决,偶尔还是会出现这个问题。

而且实际上removeOverlay 这个语句本身是有意义的,屏蔽了这个代码会造成clusterer 出现了,但是marker 不消失的问题(偶尔出现,未仔细测试)。

其实诱发这个问题的原因是在于     

map.removeOverlay(marker),在这句话执行的时候,会自动将marker.label =null, 由于Map 这个js 是百度自身摇匀了的,不是太好分析,但是我们可以在Clusterer 处理的所有地方都先读出来Label,等remove完毕之后再将这个Maker.setLabel 过去就ok了

tmplabel = this._markers.getLabel();
this._markers.getMap() && this._map.removeOverlay(this._markers);
this._markers.setLabel(tmplabel);

知道了诱因,就ok了,我在所有的removeOverlay  Marker 都加入了上面的语句,之后   就可以“完美解决”这个问题了。ps    想用map的RemoveOverlay 事件,但是在他响应这个事件的时候,label 已经空了,所以不行 

最后,贴上我修改之后的MarkClusterer.js 代码

/**
 * @fileoverview MarkerClusterer标记聚合器用来解决加载大量点要素到地图上产生覆盖现象的问题,并提高性能。
 * 主入口类是<a href="symbols/BMapLib.MarkerClusterer.html">MarkerClusterer</a>,
 * 基于Baidu Map API 1.2。
 *
 * @author Baidu Map Api Group 
 * @version 1.2
 */
  
 
/** 
 * @namespace BMap的所有library类均放在BMapLib命名空间下
 */
var BMapLib = window.BMapLib = BMapLib || {};
(function(){
     
    /**
     * 获取一个扩展的视图范围,把上下左右都扩大一样的像素值。
     * @param {Map} map BMap.Map的实例化对象
     * @param {BMap.Bounds} bounds BMap.Bounds的实例化对象
     * @param {Number} gridSize 要扩大的像素值
     *
     * @return {BMap.Bounds} 返回扩大后的视图范围。
     */
    var getExtendedBounds = function(map, bounds, gridSize){
        bounds = cutBoundsInRange(bounds);
        var pixelNE = map.pointToPixel(bounds.getNorthEast());
        var pixelSW = map.pointToPixel(bounds.getSouthWest()); 
        pixelNE.x += gridSize;
        pixelNE.y -= gridSize;
        pixelSW.x -= gridSize;
        pixelSW.y += gridSize;
        var newNE = map.pixelToPoint(pixelNE);
        var newSW = map.pixelToPoint(pixelSW);
        return new BMap.Bounds(newSW, newNE);
    };
 
    /**
     * 按照百度地图支持的世界范围对bounds进行边界处理
     * @param {BMap.Bounds} bounds BMap.Bounds的实例化对象
     *
     * @return {BMap.Bounds} 返回不越界的视图范围
     */
    var cutBoundsInRange = function (bounds) {
        var maxX = getRange(bounds.getNorthEast().lng, -180, 180);
        var minX = getRange(bounds.getSouthWest().lng, -180, 180);
        var maxY = getRange(bounds.getNorthEast().lat, -74, 74);
        var minY = getRange(bounds.getSouthWest().lat, -74, 74);
        return new BMap.Bounds(new BMap.Point(minX, minY), new BMap.Point(maxX, maxY));
    }; 
 
    /**
     * 对单个值进行边界处理。
     * @param {Number} i 要处理的数值
     * @param {Number} min 下边界值
     * @param {Number} max 上边界值
     * 
     * @return {Number} 返回不越界的数值
     */
    var getRange = function (i, mix, max) {
        mix && (i = Math.max(i, mix));
        max && (i = Math.min(i, max));
        return i;
    };
 
    /**
     * 判断给定的对象是否为数组
     * @param {Object} source 要测试的对象
     *
     * @return {Boolean} 如果是数组返回true,否则返回false
     */
    var isArray = function (source) {
        return '[object Array]' === Object.prototype.toString.call(source);
    };
 
    /**
     * 返回item在source中的索引位置
     * @param {Object} item 要测试的对象
     * @param {Array} source 数组
     *
     * @return {Number} 如果在数组内,返回索引,否则返回-1
     */
    var indexOf = function(item, source){
        var index = -1;
        if(isArray(source)){
            if (source.indexOf) {
                index = source.indexOf(item);
            } else {
                for (var i = 0, m; m = source[i]; i++) {
                    if (m === item) {
                        index = i;
                        break;
                    }
                }
            }
        }        
        return index;
    };
 
    /**
     *@exports MarkerClusterer as BMapLib.MarkerClusterer
     */
    var MarkerClusterer =  
        /**
         * MarkerClusterer
         * @class 用来解决加载大量点要素到地图上产生覆盖现象的问题,并提高性能
         * @constructor
         * @param {Map} map 地图的一个实例。
         * @param {Json Object} options 可选参数,可选项包括:

         *    markers {Array<Marker>} 要聚合的标记数组

         *    girdSize {Number} 聚合计算时网格的像素大小,默认60

         *    maxZoom {Number} 最大的聚合级别,大于该级别就不进行相应的聚合

         *    minClusterSize {Number} 最小的聚合数量,小于该数量的不能成为一个聚合,默认为2

         *    isAverangeCenter {Boolean} 聚合点的落脚位置是否是所有聚合在内点的平均值,默认为否,落脚在聚合内的第一个点

         *    styles {Array<IconStyle>} 自定义聚合后的图标风格,请参考TextIconOverlay类

         */
        BMapLib.MarkerClusterer = function(map, options){
            if (!map){
                return;
            }
            this._map = map;
            this._markers = [];
            this._clusters = [];
             
            var opts = options || {};
            this._gridSize = opts["gridSize"] || 60;
            this._maxZoom = opts["maxZoom"] || 18;
            this._minClusterSize = opts["minClusterSize"] || 2;           
            this._isAverageCenter = false;
            if (opts['isAverageCenter'] != undefined) {
                this._isAverageCenter = opts['isAverageCenter'];
            }    
            this._styles = opts["styles"] || [];
         
            var that = this;
            this._map.addEventListener("zoomend",function(){
                that._redraw();     
            });
     
            this._map.addEventListener("moveend",function(){
                 that._redraw();     
            });
 
 
 
    
            var mkrs = opts["markers"];
            isArray(mkrs) && this.addMarkers(mkrs);
        };
 
    /**
     * 添加要聚合的标记数组。
     * @param {Array<Marker>} markers 要聚合的标记数组
     *
     * @return 无返回值。
     */
    MarkerClusterer.prototype.addMarkers = function(markers){
        for(var i = 0, len = markers.length; i <len ; i++){
            this._pushMarkerTo(markers[i]);
        }
        this._createClusters();   
    };
 
    /**
     * 把一个标记添加到要聚合的标记数组中
     * @param {BMap.Marker} marker 要添加的标记
     *
     * @return 无返回值。
     */
    MarkerClusterer.prototype._pushMarkerTo = function(marker){
        var index = indexOf(marker, this._markers);
        if(index === -1){
            marker.isInCluster = false;
            this._markers.push(marker);//Marker拖放后enableDragging不做变化,忽略
        }
    };
 
    /**
     * 添加一个聚合的标记。
     * @param {BMap.Marker} marker 要聚合的单个标记。
     * @return 无返回值。
     */
    MarkerClusterer.prototype.addMarker = function(marker) {
        this._pushMarkerTo(marker);
        this._createClusters();
    };
 
    /**
     * 根据所给定的标记,创建聚合点
     * @return 无返回值
     */
    MarkerClusterer.prototype._createClusters = function(){
        var mapBounds = this._map.getBounds();
        var extendedBounds = getExtendedBounds(this._map, mapBounds, this._gridSize);
        for(var i = 0, marker; marker = this._markers[i]; i++){
            if(!marker.isInCluster && extendedBounds.containsPoint(marker.getPosition()) ){ 
                this._addToClosestCluster(marker);
            }
        }   
    };
 
    /**
     * 根据标记的位置,把它添加到最近的聚合中
     * @param {BMap.Marker} marker 要进行聚合的单个标记
     *
     * @return 无返回值。
     */
    MarkerClusterer.prototype._addToClosestCluster = function (marker){
        var distance = 4000000;
        var clusterToAddTo = null;
        var position = marker.getPosition();
        for(var i = 0, cluster; cluster = this._clusters[i]; i++){
            var center = cluster.getCenter();
            if(center){
                var d = this._map.getDistance(center, marker.getPosition());
                if(d < distance){
                    distance = d;
                    clusterToAddTo = cluster;
                }
            }
        }
     
        if (clusterToAddTo && clusterToAddTo.isMarkerInClusterBounds(marker)){
            clusterToAddTo.addMarker(marker);
        } else {
            var cluster = new Cluster(this);
            cluster.addMarker(marker);            
            this._clusters.push(cluster);
        }    
    };
 
    /**
     * 清除上一次的聚合的结果
     * @return 无返回值。
     */
    MarkerClusterer.prototype._clearLastClusters = function(){
        for(var i = 0, cluster; cluster = this._clusters[i]; i++){            
            cluster.remove();
        }
        this._clusters = [];//置空Cluster数组
        this._removeMarkersFromCluster();//把Marker的cluster标记设为false
    };
 
    /**
     * 清除某个聚合中的所有标记
     * @return 无返回值
     */
    MarkerClusterer.prototype._removeMarkersFromCluster = function(){
        for(var i = 0, marker; marker = this._markers[i]; i++){
            marker.isInCluster = false;
        }
    };
    
    /**
     * 把所有的标记从地图上清除
     * @return 无返回值
     */
    MarkerClusterer.prototype._removeMarkersFromMap = function(){
        for(var i = 0, marker; marker = this._markers[i]; i++){
            marker.isInCluster = false;
            tmplabel = marker.getLabel();
            this._map.removeOverlay(marker);       
            marker.setLabel(tmplabel);
        }
    };
 
    /**
     * 删除单个标记
     * @param {BMap.Marker} marker 需要被删除的marker
     *
     * @return {Boolean} 删除成功返回true,否则返回false
     */
    MarkerClusterer.prototype._removeMarker = function(marker) {
        var index = indexOf(marker, this._markers);
        if (index === -1) {
            return false;
        }
        tmplabel = marker.getLabel();
        this._map.removeOverlay(marker);
        marker.setLabel(tmplabel);
        this._markers.splice(index, 1);
        return true;
    };
 
    /**
     * 删除单个标记
     * @param {BMap.Marker} marker 需要被删除的marker
     *
     * @return {Boolean} 删除成功返回true,否则返回false
     */
    MarkerClusterer.prototype.removeMarker = function(marker) {
        var success = this._removeMarker(marker);
        if (success) {
            this._clearLastClusters();
            this._createClusters();
        }
        return success;
    };
     
    /**
     * 删除一组标记
     * @param {Array<BMap.Marker>} markers 需要被删除的marker数组
     *
     * @return {Boolean} 删除成功返回true,否则返回false
     */
    MarkerClusterer.prototype.removeMarkers = function(markers) {
        var success = false;
        for (var i = 0; i < markers.length; i++) {
            var r = this._removeMarker(markers[i]);
            success = success || r; 
        }
 
        if (success) {
            this._clearLastClusters();
            this._createClusters();
        }
        return success;
    };
 
    /**
     * 从地图上彻底清除所有的标记
     * @return 无返回值
     */
    MarkerClusterer.prototype.clearMarkers = function() {
        this._clearLastClusters();
        this._removeMarkersFromMap();
        this._markers = [];
    };
 
    /**
     * 重新生成,比如改变了属性等
     * @return 无返回值
     */
    MarkerClusterer.prototype._redraw = function () {
        this._clearLastClusters();
        this._createClusters();
    };
 
    /**
     * 获取网格大小
     * @return {Number} 网格大小
     */
    MarkerClusterer.prototype.getGridSize = function() {
        return this._gridSize;
    };
 
    /**
     * 设置网格大小
     * @param {Number} size 网格大小
     * @return 无返回值
     */
    MarkerClusterer.prototype.setGridSize = function(size) {
        this._gridSize = size;
        this._redraw();
    };
 
    /**
     * 获取聚合的最大缩放级别。
     * @return {Number} 聚合的最大缩放级别。
     */
    MarkerClusterer.prototype.getMaxZoom = function() {
        return this._maxZoom;       
    };
 
    /**
     * 设置聚合的最大缩放级别
     * @param {Number} maxZoom 聚合的最大缩放级别
     * @return 无返回值
     */
    MarkerClusterer.prototype.setMaxZoom = function(maxZoom) {
        this._maxZoom = maxZoom;
        this._redraw();
    };
 
    /**
     * 获取聚合的样式风格集合
     * @return {Array<IconStyle>} 聚合的样式风格集合
     */
    MarkerClusterer.prototype.getStyles = function() {
        return this._styles;
    };
 
    /**
     * 设置聚合的样式风格集合
     * @param {Array<IconStyle>} styles 样式风格数组
     * @return 无返回值
     */
    MarkerClusterer.prototype.setStyles = function(styles) {
        this._styles = styles;
        this._redraw();
    };
 
    /**
     * 获取单个聚合的最小数量。
     * @return {Number} 单个聚合的最小数量。
     */
    MarkerClusterer.prototype.getMinClusterSize = function() {
        return this._minClusterSize;
    };
 
    /**
     * 设置单个聚合的最小数量。
     * @param {Number} size 单个聚合的最小数量。
     * @return 无返回值。
     */
    MarkerClusterer.prototype.setMinClusterSize = function(size) {
        this._minClusterSize = size;
        this._redraw();
    };
 
    /**
     * 获取单个聚合的落脚点是否是聚合内所有标记的平均中心。
     * @return {Boolean} true或false。
     */
    MarkerClusterer.prototype.isAverageCenter = function() {
        return this._isAverageCenter;
    };
 
    /**
     * 获取聚合的Map实例。
     * @return {Map} Map的示例。
     */
    MarkerClusterer.prototype.getMap = function() {
      return this._map;
    };
 
    /**
     * 获取所有的标记数组。
     * @return {Array<Marker>} 标记数组。
     */
    MarkerClusterer.prototype.getMarkers = function() {
        return this._markers;
    };
 
    /**
     * 获取聚合的总数量。
     * @return {Number} 聚合的总数量。
     */
    MarkerClusterer.prototype.getClustersCount = function() {
        var count = 0;
        for(var i = 0, cluster; cluster = this._clusters[i]; i++){
            cluster.isReal() && count++;     
        }
        return count;
    };
 
    /**
     * @ignore
     * Cluster
     * @class 表示一个聚合对象,该聚合,包含有N个标记,这N个标记组成的范围,并有予以显示在Map上的TextIconOverlay等。
     * @constructor
     * @param {MarkerClusterer} markerClusterer 一个标记聚合器示例。
     */
    function Cluster(markerClusterer){
        this._markerClusterer = markerClusterer;
        this._map = markerClusterer.getMap();
        this._minClusterSize = markerClusterer.getMinClusterSize();
        this._isAverageCenter = markerClusterer.isAverageCenter();
        this._center = null;//落脚位置
        this._markers = [];//这个Cluster中所包含的markers
        this._gridBounds = null;//以中心点为准,向四边扩大gridSize个像素的范围,也即网格范围
        this._isReal = false; //真的是个聚合
     
        this._clusterMarker = new BMapLib.TextIconOverlay(this._center, this._markers.length, {"styles":this._markerClusterer.getStyles()});
        //this._map.addOverlay(this._clusterMarker);
    }
    
    /**
     * 向该聚合添加一个标记。
     * @param {Marker} marker 要添加的标记。
     * @return 无返回值。
     */
    Cluster.prototype.addMarker = function(marker){
        if(this.isMarkerInCluster(marker)){
            return false;
        }//也可用marker.isInCluster判断,外面判断OK,这里基本不会命中
     
        if (!this._center){
            this._center = marker.getPosition();
            this.updateGridBounds();//
        } else {
            if(this._isAverageCenter){
                var l = this._markers.length + 1;
                var lat = (this._center.lat * (l - 1) + marker.getPosition().lat) / l;
                var lng = (this._center.lng * (l - 1) + marker.getPosition().lng) / l;
                this._center = new BMap.Point(lng, lat);
                this.updateGridBounds();
            }//计算新的Center
        }
     
        marker.isInCluster = true;
        this._markers.push(marker);
     
        var len = this._markers.length;
        if(len < this._minClusterSize ){     
            this._map.addOverlay(marker);
            //this.updateClusterMarker();
            return true;
        } else if (len === this._minClusterSize) {
            for (var i = 0; i < len; i++) {
                tmplabel = this._markers[i].getLabel();
                this._markers[i].getMap() && this._map.removeOverlay(this._markers[i]);
                this._markers[i].setLabel(tmplabel);
            }
             
        } 
        this._map.addOverlay(this._clusterMarker);
        this._isReal = true;
        this.updateClusterMarker();
        return true;
    };
     
    /**
     * 判断一个标记是否在该聚合中。
     * @param {Marker} marker 要判断的标记。
     * @return {Boolean} true或false。
     */
    Cluster.prototype.isMarkerInCluster= function(marker){
        if (this._markers.indexOf) {
            return this._markers.indexOf(marker) != -1;
        } else {
            for (var i = 0, m; m = this._markers[i]; i++) {
                if (m === marker) {
                    return true;
                }
            }
        }
        return false;
    };
 
    /**
     * 判断一个标记是否在该聚合网格范围中。
     * @param {Marker} marker 要判断的标记。
     * @return {Boolean} true或false。
     */
    Cluster.prototype.isMarkerInClusterBounds = function(marker) {
        return this._gridBounds.containsPoint(marker.getPosition());
    };
     
    Cluster.prototype.isReal = function(marker) {
        return this._isReal;
    };
 
    /**
     * 更新该聚合的网格范围。
     * @return 无返回值。
     */
    Cluster.prototype.updateGridBounds = function() {
        var bounds = new BMap.Bounds(this._center, this._center);
        this._gridBounds = getExtendedBounds(this._map, bounds, this._markerClusterer.getGridSize());
    };
 
    /**
     * 更新该聚合的显示样式,也即TextIconOverlay。
     * @return 无返回值。
     */
    Cluster.prototype.updateClusterMarker = function () {
        if (this._map.getZoom() > this._markerClusterer.getMaxZoom()) {
            this._clusterMarker && this._map.removeOverlay(this._clusterMarker);
            for (var i = 0, marker; marker = this._markers[i]; i++) {
                this._map.addOverlay(marker);
            }
            return;
        }
 
        if (this._markers.length < this._minClusterSize) {
            this._clusterMarker.hide();
            return;
        }
 
        this._clusterMarker.setPosition(this._center);
         
        this._clusterMarker.setText(this._markers.length);
 
        var thatMap = this._map;
        var thatBounds = this.getBounds();
        this._clusterMarker.addEventListener("click", function(event){
            thatMap.setViewport(thatBounds);
        });
 
    };
 
    /**
     * 删除该聚合。
     * @return 无返回值。
     */
    Cluster.prototype.remove = function(){
        for (var i = 0, m; m = this._markers[i]; i++) {
                var tmplabel = this._markers[i].getLabel();
                this._markers[i].getMap() && this._map.removeOverlay(this._markers[i]);
                this._markers[i].setLabel(tmplabel);
 
        }//清除散的标记点
        this._map.removeOverlay(this._clusterMarker);
        this._markers.length = 0;
        delete this._markers;
    }
 
    /**
     * 获取该聚合所包含的所有标记的最小外接矩形的范围。
     * @return {BMap.Bounds} 计算出的范围。
     */
    Cluster.prototype.getBounds = function() {
        var bounds = new BMap.Bounds(this._center,this._center);
        for (var i = 0, marker; marker = this._markers[i]; i++) {
            bounds.extend(marker.getPosition());
        }
        return bounds;
    };
 
    /**
     * 获取该聚合的落脚点。
     * @return {BMap.Point} 该聚合的落脚点。
     */
    Cluster.prototype.getCenter = function() {
        return this._center;
    };
 
})();

原解决方案作者:http://www.cnblogs.com/jicheng1014/p/3143859.html
附上压缩过的代码:

var BMapLib=window.BMapLib=BMapLib||{};!function(){function t(t){this._markerClusterer=t,this._map=t.getMap(),this._minClusterSize=t.getMinClusterSize(),this._isAverageCenter=t.isAverageCenter(),this._center=null,this._markers=[],this._gridBounds=null,this._isReal=!1,this._clusterMarker=new BMapLib.TextIconOverlay(this._center,this._markers.length,{styles:this._markerClusterer.getStyles()})}var e=function(t,e,s){e=r(e);var i=t.pointToPixel(e.getNorthEast()),a=t.pointToPixel(e.getSouthWest());i.x+=s,i.y-=s,a.x-=s,a.y+=s;var n=t.pixelToPoint(i),o=t.pixelToPoint(a);return new BMap.Bounds(o,n)},r=function(t){var e=s(t.getNorthEast().lng,-180,180),r=s(t.getSouthWest().lng,-180,180),i=s(t.getNorthEast().lat,-74,74),a=s(t.getSouthWest().lat,-74,74);return new BMap.Bounds(new BMap.Point(r,a),new BMap.Point(e,i))},s=function(t,e,r){return e&&(t=Math.max(t,e)),r&&(t=Math.min(t,r)),t},i=function(t){return"[object Array]"===Object.prototype.toString.call(t)},a=function(t,e){var r=-1;if(i(e))if(e.indexOf)r=e.indexOf(t);else for(var s,a=0;s=e[a];a++)if(s===t){r=a;break}return r},n=BMapLib.MarkerClusterer=function(t,e){if(t){this._map=t,this._markers=[],this._clusters=[];var r=e||{};this._gridSize=r.gridSize||60,this._maxZoom=r.maxZoom||18,this._minClusterSize=r.minClusterSize||2,this._isAverageCenter=!1,void 0!=r.isAverageCenter&&(this._isAverageCenter=r.isAverageCenter),this._styles=r.styles||[];var s=this;this._map.addEventListener("zoomend",function(){s._redraw()}),this._map.addEventListener("moveend",function(){s._redraw()});var a=r.markers;i(a)&&this.addMarkers(a)}};n.prototype.addMarkers=function(t){for(var e=0,r=t.length;r>e;e++)this._pushMarkerTo(t[e]);this._createClusters()},n.prototype._pushMarkerTo=function(t){var e=a(t,this._markers);-1===e&&(t.isInCluster=!1,this._markers.push(t))},n.prototype.addMarker=function(t){this._pushMarkerTo(t),this._createClusters()},n.prototype._createClusters=function(){for(var t,r=this._map.getBounds(),s=e(this._map,r,this._gridSize),i=0;t=this._markers[i];i++)!t.isInCluster&&s.containsPoint(t.getPosition())&&this._addToClosestCluster(t)},n.prototype._addToClosestCluster=function(e){for(var r,s=4e6,i=null,a=(e.getPosition(),0);r=this._clusters[a];a++){var n=r.getCenter();if(n){var o=this._map.getDistance(n,e.getPosition());s>o&&(s=o,i=r)}}if(i&&i.isMarkerInClusterBounds(e))i.addMarker(e);else{var r=new t(this);r.addMarker(e),this._clusters.push(r)}},n.prototype._clearLastClusters=function(){for(var t,e=0;t=this._clusters[e];e++)t.remove();this._clusters=[],this._removeMarkersFromCluster()},n.prototype._removeMarkersFromCluster=function(){for(var t,e=0;t=this._markers[e];e++)t.isInCluster=!1},n.prototype._removeMarkersFromMap=function(){for(var t,e=0;t=this._markers[e];e++)t.isInCluster=!1,tmplabel=t.getLabel(),this._map.removeOverlay(t),t.setLabel(tmplabel)},n.prototype._removeMarker=function(t){var e=a(t,this._markers);return-1===e?!1:(tmplabel=t.getLabel(),this._map.removeOverlay(t),t.setLabel(tmplabel),this._markers.splice(e,1),!0)},n.prototype.removeMarker=function(t){var e=this._removeMarker(t);return e&&(this._clearLastClusters(),this._createClusters()),e},n.prototype.removeMarkers=function(t){for(var e=!1,r=0;r<t.length;r++){var s=this._removeMarker(t[r]);e=e||s}return e&&(this._clearLastClusters(),this._createClusters()),e},n.prototype.clearMarkers=function(){this._clearLastClusters(),this._removeMarkersFromMap(),this._markers=[]},n.prototype._redraw=function(){this._clearLastClusters(),this._createClusters()},n.prototype.getGridSize=function(){return this._gridSize},n.prototype.setGridSize=function(t){this._gridSize=t,this._redraw()},n.prototype.getMaxZoom=function(){return this._maxZoom},n.prototype.setMaxZoom=function(t){this._maxZoom=t,this._redraw()},n.prototype.getStyles=function(){return this._styles},n.prototype.setStyles=function(t){this._styles=t,this._redraw()},n.prototype.getMinClusterSize=function(){return this._minClusterSize},n.prototype.setMinClusterSize=function(t){this._minClusterSize=t,this._redraw()},n.prototype.isAverageCenter=function(){return this._isAverageCenter},n.prototype.getMap=function(){return this._map},n.prototype.getMarkers=function(){return this._markers},n.prototype.getClustersCount=function(){for(var t,e=0,r=0;t=this._clusters[r];r++)t.isReal()&&e++;return e},t.prototype.addMarker=function(t){if(this.isMarkerInCluster(t))return!1;if(this._center){if(this._isAverageCenter){var e=this._markers.length+1,r=(this._center.lat*(e-1)+t.getPosition().lat)/e,s=(this._center.lng*(e-1)+t.getPosition().lng)/e;this._center=new BMap.Point(s,r),this.updateGridBounds()}}else this._center=t.getPosition(),this.updateGridBounds();t.isInCluster=!0,this._markers.push(t);var i=this._markers.length;if(i<this._minClusterSize)return this._map.addOverlay(t),!0;if(i===this._minClusterSize)for(var a=0;i>a;a++)tmplabel=this._markers[a].getLabel(),this._markers[a].getMap()&&this._map.removeOverlay(this._markers[a]),this._markers[a].setLabel(tmplabel);return this._map.addOverlay(this._clusterMarker),this._isReal=!0,this.updateClusterMarker(),!0},t.prototype.isMarkerInCluster=function(t){if(this._markers.indexOf)return-1!=this._markers.indexOf(t);for(var e,r=0;e=this._markers[r];r++)if(e===t)return!0;return!1},t.prototype.isMarkerInClusterBounds=function(t){return this._gridBounds.containsPoint(t.getPosition())},t.prototype.isReal=function(){return this._isReal},t.prototype.updateGridBounds=function(){var t=new BMap.Bounds(this._center,this._center);this._gridBounds=e(this._map,t,this._markerClusterer.getGridSize())},t.prototype.updateClusterMarker=function(){if(this._map.getZoom()>this._markerClusterer.getMaxZoom()){this._clusterMarker&&this._map.removeOverlay(this._clusterMarker);for(var t,e=0;t=this._markers[e];e++)this._map.addOverlay(t)}else{if(this._markers.length<this._minClusterSize)return this._clusterMarker.hide(),void 0;this._clusterMarker.setPosition(this._center),this._clusterMarker.setText(this._markers.length);var r=this._map,s=this.getBounds();this._clusterMarker.addEventListener("click",function(){r.setViewport(s)})}},t.prototype.remove=function(){for(var t,e=0;t=this._markers[e];e++){var r=this._markers[e].getLabel();this._markers[e].getMap()&&this._map.removeOverlay(this._markers[e]),this._markers[e].setLabel(r)}this._map.removeOverlay(this._clusterMarker),this._markers.length=0,delete this._markers},t.prototype.getBounds=function(){for(var t,e=new BMap.Bounds(this._center,this._center),r=0;t=this._markers[r];r++)e.extend(t.getPosition());return e},t.prototype.getCenter=function(){return this._center}}();

附效果图:

20131218100851.jpg

转载于:https://www.cnblogs.com/dengyukeji/p/3834668.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值