百度地图点聚合缩放,label标签消失

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:44   2338   0
1.本地新建js文件MarkerClusterer.js,内容如下:
var BMapLib = window.BMapLib = BMapLib || {}; (function() {
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)
};
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))
};
var getRange = function(i, mix, max) {
mix && (i = Math.max(i, mix));
max && (i = Math.min(i, max));
return i
};
var isArray = function(source) {
return '[object Array]' === Object.prototype.toString.call(source)
};
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
};
var MarkerClusterer = 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)
};
MarkerClusterer.prototype.addMarkers = function(markers) {
for (var i = 0,
len = markers.length; i < len; i++) {
this._pushMarkerTo(markers[i])
}
this._createClusters()
};
MarkerClusterer.prototype._pushMarkerTo = function(marker) {
var index = indexOf(marker, this._markers);
if (index === -1) {
marker.isInCluster = false;
this._markers.push(marker)
}
};
MarkerClusterer.prototype.addMarker = function(marker) {
this._pushMarkerTo(marker);
this._createClusters()
};
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)
}
}
};
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)) { this._center = new BMap.Point(lng, lat);
this.updateGridBounds()
}
}
marker.isInCluster = true;
this._markers.push(marker);
var len = this._markers.length;
if (len < this._minClusterSize) {
this._map.addOverlay(marker);
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
};
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
};
Cluster.prototype.isMarkerInClusterBounds = function(marker) {
return this._gridBounds.containsPoint(marker.getPosition())
};
Cluster.prototype.isReal = function(marker) {
return this._isReal
};
Cluster.prototype.updateGridBounds = function() {
var bounds = new BMap.Bounds(this._center, this._center);
this._gridBounds = getExtendedBounds(this._map, bounds, this._markerClusterer.getGridSize())
};
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)
})
};
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
};
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
};
Cluster.prototype.getCenter = function() {
return this._center;
};
})();


2.将http://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js引用更改为本地路径MarkerClusterer.js文件


原因:百度地图点聚合removeOverlay时,将label删改了,上面文件在删除前,将label值取出,删除后再赋值。本人没研究,找别人的文章看的原因。经验证,点聚合缩放时,label值存在
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP