123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788 |
- 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._getCent = opts["getCent"];
- this._geoc = opts["geoc"];
- this._ladata = opts["ladata"];
- this._gridSize = opts["gridSize"] || 60;
- this._maxZoom = opts["maxZoom"] || 18;
- this._minClusterSize = opts["minClusterSize"] || 1;
- 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.getTitle = function(){
- return this._getCent;
- };
- MarkerClusterer.prototype.getGeoc = function(){
- return this._geoc;
- };
- MarkerClusterer.prototype.clusters = function(){
- return this._clusters;
- };
- MarkerClusterer.prototype.ladata = function(){
- return this._ladata;
- };
-
- 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);
- }
- }
-
- var len = this._markers.length;
- for (var i = 0; i < len; i++) {
- if(this._clusters[i]){
- this._clusters[i].render();
- }
- }
-
- };
-
- 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);
- }
- };
-
- MarkerClusterer.prototype._clearLastClusters = function(){
- for(var i = 0, cluster; cluster = this._clusters[i]; i++){
- cluster.remove();
- }
- this._clusters = [];
- this._removeMarkersFromCluster();
- };
-
- MarkerClusterer.prototype._removeMarkersFromCluster = function(){
- for(var i = 0, marker; marker = this._markers[i]; i++){
- var u = mp.getZoom();
- if(u <= 12){
- mp.clearOverlays();
- }
- marker.isInCluster = false;
- }
- };
-
- MarkerClusterer.prototype._removeMarkersFromMap = function(){
-
- for(var i = 0, marker; marker = this._markers[i]; i++){
- var u = mp.getZoom();
- if(u <= 12){
- mp.clearOverlays();
- }
- marker.isInCluster = false;
- this._map.removeOverlay(marker);
- }
- };
-
- MarkerClusterer.prototype._removeMarker = function(marker) {
- var index = indexOf(marker, this._markers);
- if (index === -1) {
- return false;
- }
- var u = mp.getZoom();
- if(u <= 12){
- mp.clearOverlays();
- }
- this._map.removeOverlay(marker);
- this._markers.splice(index, 1);
- return true;
- };
-
- MarkerClusterer.prototype.removeMarker = function(marker) {
- var success = this._removeMarker(marker);
- if (success) {
- this._clearLastClusters();
- this._createClusters();
- }
- return success;
- };
-
- 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;
- };
-
- MarkerClusterer.prototype.clearMarkers = function() {
- var u = mp.getZoom();
- if(u <= 12){
- mp.clearOverlays();
- }
- this._clearLastClusters();
- this._removeMarkersFromMap();
- this._markers = [];
- };
-
- MarkerClusterer.prototype._redraw = function () {
- this._clearLastClusters();
-
- this._createClusters();
- };
-
- MarkerClusterer.prototype.getGridSize = function() {
- return this._gridSize;
- };
-
- MarkerClusterer.prototype.setGridSize = function(size) {
- this._gridSize = size;
- this._redraw();
- };
-
- MarkerClusterer.prototype.getMaxZoom = function() {
- return this._maxZoom;
- };
-
- MarkerClusterer.prototype.setMaxZoom = function(maxZoom) {
- this._maxZoom = maxZoom;
- this._redraw();
- };
-
- MarkerClusterer.prototype.getStyles = function() {
- return this._styles;
- };
-
- MarkerClusterer.prototype.setStyles = function(styles) {
- this._styles = styles;
- this._redraw();
- };
-
- MarkerClusterer.prototype.getMinClusterSize = function() {
- return this._minClusterSize;
- };
-
- MarkerClusterer.prototype.setMinClusterSize = function(size) {
- this._minClusterSize = size;
- this._redraw();
- };
-
- MarkerClusterer.prototype.isAverageCenter = function() {
- return this._isAverageCenter;
- };
-
- MarkerClusterer.prototype.getMap = function() {
- return this._map;
- };
-
- MarkerClusterer.prototype.getMarkers = function() {
- return this._markers;
- };
-
- MarkerClusterer.prototype.getClustersCount = function() {
- var count = 0;
- for(var i = 0, cluster; cluster = this._clusters[i]; i++){
- cluster.isReal() && count++;
- }
- return count;
- };
-
- function Cluster(markerClusterer){
- this._markerClusterer = markerClusterer;
- this._map = markerClusterer.getMap();
- this._text=markerClusterer.getTitle()
- this._geoc=markerClusterer.getGeoc()
- this._clusters=markerClusterer.clusters()
- this._lad=markerClusterer.ladata()
- this._minClusterSize = markerClusterer.getMinClusterSize();
- this._isAverageCenter = markerClusterer.isAverageCenter();
- this._center = null;
- this._markers = [];
- this._gridBounds = null;
- this._showPosition = null;
- this._isReal = false;
- this._styles = markerClusterer.getStyles();
- this._labels = [];
- this._clusterMarker = new BMapLib.TextIconOverlay(this._center, {name:'区域',value : this._markers.length}, {"styles":this._markerClusterer.getStyles()});
-
-
- }
-
-
-
- Cluster.prototype.addMarker = function(marker){
- if(this.isMarkerInCluster(marker)){
- return false;
- }
- 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();
- }
- }
-
- marker.isInCluster = true;
- this._markers.push(marker);
-
-
-
- };
-
-
- Cluster.prototype.render = function(){
- var len = this._markers.length;
-
- if (len < this._minClusterSize) {
- for (var i = 0; i < len; i++) {
- this._map.addOverlay(this._markers[i]);
- }
- } else {
- this._map.addOverlay(this._clusterMarker);
- this._isReal = true;
- this.updateClusterMarker();
- }
- }
-
- 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.addLabel = function (marker,title) {
-
- var position = marker.getPosition();
-
-
- var label = new BMap.Label({position : position});
- label.setStyle({
- height : '25px',
- color : "#fff",
- backgroundColor : this._styles[0].backgroundColor,
- border : 'none',
- borderRadius : "25px",
- fontWeight : 'bold',
- });
- var content = '<span style="color:'+this._styles[0].backgroundColor+'"><i class="fa fa-map-marker"></i></span>'+'<p style="padding:0px 13px;text-align:center;margin-top:5px;">'+title+'</p>';
- label.setContent(content)
- label.setPosition(position);
- this._labels.push(label);
- this._map.addOverlay(label);
- }
-
- 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._lad[i]; i++) {
- var title =this._lad[i].title;
- marker = new BMap.Marker(this._lad[i].point);
-
- }
- return;
- }
-
- if (this._markers.length < this._minClusterSize) {
- this._clusterMarker.hide();
- return;
- }
- this._clusterMarker.setPosition(this._center);
-
-
- var _city,setaa,leaa,arr;
- setaa = this._clusterMarker;
- leaa = this._markers;
- arr = this._clusters;
-
- for(var i = 0,occupational;occupational = arr[i];i++){
- this._geoc.getLocation(setaa._position, function(rs){
- var addComp = rs.addressComponents;
- if(addComp.city !=''){
- _city=(addComp.city);
- }else {
- _city=(addComp.district);
- }
- setaa.setText({name : _city , value : leaa.length});
-
- });
- }
-
-
-
-
- var thatMap = this._map;
- var thatBounds = this.getBounds();
- var center = this._center;
- this._clusterMarker.addEventListener("click", function(event){
-
-
-
- var zoom = thatMap.getZoom();
- zoom = zoom > 14 ? zoom : 14;
- thatMap.setZoom(zoom);
- thatMap.setCenter(center);
- });
-
- };
-
- Cluster.prototype.remove = function(){
- for (var i = 0, m; m = this._labels[i]; i++) {
- this._map.removeOverlay(m);
- }
- 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;
- };
-
- })();
|