SGGL_JT/SUBQHSE/FineUIPro.Web/res/map/MarkerClusterer.js

1 line
6.4 KiB
JavaScript
Raw Normal View History

2025-04-07 17:43:30 +08:00
var BMapLib=window.BMapLib=BMapLib||{};(function(){var b=function(m,l,j){l=d(l);var n=m.pointToPixel(l.getNorthEast());var i=m.pointToPixel(l.getSouthWest());n.x+=j;n.y-=j;i.x-=j;i.y+=j;var h=m.pixelToPoint(n);var k=m.pixelToPoint(i);return new BMap.Bounds(k,h)};var d=function(i){var k=f(i.getNorthEast().lng,-180,180);var h=f(i.getSouthWest().lng,-180,180);var j=f(i.getNorthEast().lat,-74,74);var l=f(i.getSouthWest().lat,-74,74);return new BMap.Bounds(new BMap.Point(h,l),new BMap.Point(k,j))};var f=function(j,k,h){k&&(j=Math.max(j,k));h&&(j=Math.min(j,h));return j};var a=function(h){return"[object Array]"===Object.prototype.toString.call(h)};var c=function(l,n){var j=-1;if(a(n)){if(n.indexOf){j=n.indexOf(l)}else{for(var k=0,h;h=n[k];k++){if(h===l){j=k;break}}}}return j};var e=BMapLib.MarkerClusterer=function(l,h){if(!l){return}this._map=l;this._markers=[];this._clusters=[];var k=h||{};this._gridSize=k.gridSize||60;this._maxZoom=k.maxZoom||18;this._minClusterSize=k.minClusterSize||2;this._isAverageCenter=false;if(k.isAverageCenter!=undefined){this._isAverageCenter=k.isAverageCenter}this._styles=k.styles||[];var j=this;this._map.addEventListener("zoomend",function(){j._redraw()});this._map.addEventListener("moveend",function(){j._redraw()});var i=k.markers;a(i)&&this.addMarkers(i)};e.prototype.addMarkers=function(k){for(var j=0,h=k.length;j<h;j++){this._pushMarkerTo(k[j])}this._createClusters()};e.prototype._pushMarkerTo=function(h){var i=c(h,this._markers);if(i===-1){h.isInCluster=false;this._markers.push(h)}};e.prototype.addMarker=function(h){this._pushMarkerTo(h);this._createClusters()};e.prototype._createClusters=function(){var j=this._map.getBounds();var l=b(this._map,j,this._gridSize);for(var k=0,h;h=this._markers[k];k++){if(!h.isInCluster&&l.containsPoint(h.getPosition())){this._addToClosestCluster(h)}}};e.prototype._addToClosestCluster=function(l){var p=4000000;var n=null;var k=l.getPosition();for(var m=0,j;j=this._clusters[m];m++){var h=j.getCenter();if(h){var o=this._map.getDistance(h,l.getPosition());if(o<p){p=o;n=j}}}if(n&&n.isMarkerInClusterBounds(l)){n.addMarker(l)}else{var j=new g(this);j.addMarker(l);this._clusters.push(j)}};e.prototype._clearLastClusters=function(){for(var j=0,h;h=this._clusters[j];j++){h.remove()}this._clusters=[];this._removeMarkersFromCluster()};e.prototype._removeMarkersFromCluster=function(){for(var j=0,h;h=this._markers[j];j++){h.isInCluster=false}};e.prototype._removeMarkersFromMap=function(){for(var j=0,h;h=this._markers[j];j++){h.isInCluster=false;this._map.removeOverlay(h)}};e.prototype._removeMarker=function(h){var i=c(h,this._markers);if(i===-1){return false}this._map.removeOverlay(h);this._markers.splice(i,1);return true};e.prototype.removeMarker=function(h){var i=this._removeMarker(h);if(i){this._clearLastClusters();this._createClusters()}return i};e.prototype.removeMarkers=function(l){var k=false;for(var h=0;h<l.length;h++){var j=this._removeMarker(l[h]);k=k||j}if(k){this._clearLastClusters();this._createClusters()}return k};e.prototype.clearMarkers=function(){this._clearLastClusters();this._removeMarkersFromMap();this._markers=[]};e.prototype._redraw=function(){this._clearLastClusters();this._createClusters()};e.prototype.getGridSize=function(){return this._gridSize};e.prototype.setGridSize=function(h){this._gridSize=h;this._redraw()};e.prototype.getMaxZoom=function(){return this._maxZoom};e.prototype.setMaxZoom=function(h){this._maxZoom=h;this._redraw()};e.prototype.getStyles=function(){return this._styles};e.prototype.setStyles=function(h){this._styles=h;this._redraw()};e.prototype.getMinClusterSize=function(){return this._minClusterSize};e.prototype.setMinClusterSize=function(h){this._minClusterSize=h;this._redraw()};e.prototype.isAverageCenter=function(){return this._isAverageCenter};e.prototype.getMap=function(){return this._map};e.prototype.getMarkers=function(){return this._markers};e.prototype.getClustersCount=function(){var k=0;for(var j=0,h;h=this._clusters[j];j++){h.isReal()&&k++}return k};function g(h){this._markerClusterer=h;this._map=h.getMap();this._minClust