MarkerClusterer_min.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. var BMap = window.BMap
  2. var BMapLib = (window.BMapLib = BMapLib || {})
  3. ;(function () {
  4. var b = function (m, l, j) {
  5. l = d(l)
  6. var n = m.pointToPixel(l.getNorthEast())
  7. var i = m.pointToPixel(l.getSouthWest())
  8. n.x += j
  9. n.y -= j
  10. i.x -= j
  11. i.y += j
  12. var h = m.pixelToPoint(n)
  13. var k = m.pixelToPoint(i)
  14. return new BMap.Bounds(k, h)
  15. }
  16. var d = function (i) {
  17. var k = f(i.getNorthEast().lng, -180, 180)
  18. var h = f(i.getSouthWest().lng, -180, 180)
  19. var j = f(i.getNorthEast().lat, -74, 74)
  20. var l = f(i.getSouthWest().lat, -74, 74)
  21. return new BMap.Bounds(new BMap.Point(h, l), new BMap.Point(k, j))
  22. }
  23. var f = function (j, k, h) {
  24. k && (j = Math.max(j, k))
  25. h && (j = Math.min(j, h))
  26. return j
  27. }
  28. var a = function (h) {
  29. return '[object Array]' === Object.prototype.toString.call(h)
  30. }
  31. var c = function (l, n) {
  32. var j = -1
  33. if (a(n)) {
  34. if (n.indexOf) {
  35. j = n.indexOf(l)
  36. } else {
  37. for (var k = 0, h; (h = n[k]); k++) {
  38. if (h === l) {
  39. j = k
  40. break
  41. }
  42. }
  43. }
  44. }
  45. return j
  46. }
  47. var e = (BMapLib.MarkerClusterer = function (l, h) {
  48. if (!l) {
  49. return
  50. }
  51. this._map = l
  52. this._markers = []
  53. this._clusters = []
  54. var k = h || {}
  55. this._gridSize = k.gridSize || 60
  56. this._maxZoom = k.maxZoom || 18
  57. this._minClusterSize = k.minClusterSize || 2
  58. this._isAverageCenter = false
  59. if (k.isAverageCenter != undefined) {
  60. this._isAverageCenter = k.isAverageCenter
  61. }
  62. this._styles = k.styles || []
  63. var j = this
  64. this._map.addEventListener('zoomend', function () {
  65. j._redraw()
  66. })
  67. this._map.addEventListener('moveend', function () {
  68. j._redraw()
  69. })
  70. var i = k.markers
  71. a(i) && this.addMarkers(i)
  72. })
  73. e.prototype.addMarkers = function (k) {
  74. for (var j = 0, h = k.length; j < h; j++) {
  75. this._pushMarkerTo(k[j])
  76. }
  77. this._createClusters()
  78. }
  79. e.prototype._pushMarkerTo = function (h) {
  80. var i = c(h, this._markers)
  81. if (i === -1) {
  82. h.isInCluster = false
  83. this._markers.push(h)
  84. }
  85. }
  86. e.prototype.addMarker = function (h) {
  87. this._pushMarkerTo(h)
  88. this._createClusters()
  89. }
  90. e.prototype._createClusters = function () {
  91. var j = this._map.getBounds()
  92. var l = b(this._map, j, this._gridSize)
  93. for (var k = 0, h; (h = this._markers[k]); k++) {
  94. if (!h.isInCluster && l.containsPoint(h.getPosition())) {
  95. this._addToClosestCluster(h)
  96. }
  97. }
  98. }
  99. e.prototype._addToClosestCluster = function (l) {
  100. var p = 4000000
  101. var n = null
  102. // var k = l.getPosition()
  103. for (var m = 0, j; (j = this._clusters[m]); m++) {
  104. var h = j.getCenter()
  105. if (h) {
  106. var o = this._map.getDistance(h, l.getPosition())
  107. if (o < p) {
  108. p = o
  109. n = j
  110. }
  111. }
  112. }
  113. if (n && n.isMarkerInClusterBounds(l)) {
  114. n.addMarker(l)
  115. } else {
  116. // eslint-disable-next-line
  117. var j = new g(this)
  118. j.addMarker(l)
  119. this._clusters.push(j)
  120. }
  121. }
  122. e.prototype._clearLastClusters = function () {
  123. for (var j = 0, h; (h = this._clusters[j]); j++) {
  124. h.remove()
  125. }
  126. this._clusters = []
  127. this._removeMarkersFromCluster()
  128. }
  129. e.prototype._removeMarkersFromCluster = function () {
  130. for (var j = 0, h; (h = this._markers[j]); j++) {
  131. h.isInCluster = false
  132. }
  133. }
  134. e.prototype._removeMarkersFromMap = function () {
  135. for (var j = 0, h; (h = this._markers[j]); j++) {
  136. h.isInCluster = false
  137. this._map.removeOverlay(h)
  138. }
  139. }
  140. e.prototype._removeMarker = function (h) {
  141. var i = c(h, this._markers)
  142. if (i === -1) {
  143. return false
  144. }
  145. this._map.removeOverlay(h)
  146. this._markers.splice(i, 1)
  147. return true
  148. }
  149. e.prototype.removeMarker = function (h) {
  150. var i = this._removeMarker(h)
  151. if (i) {
  152. this._clearLastClusters()
  153. this._createClusters()
  154. }
  155. return i
  156. }
  157. e.prototype.removeMarkers = function (l) {
  158. var k = false
  159. for (var h = 0; h < l.length; h++) {
  160. var j = this._removeMarker(l[h])
  161. k = k || j
  162. }
  163. if (k) {
  164. this._clearLastClusters()
  165. this._createClusters()
  166. }
  167. return k
  168. }
  169. e.prototype.clearMarkers = function () {
  170. this._clearLastClusters()
  171. this._removeMarkersFromMap()
  172. this._markers = []
  173. }
  174. e.prototype._redraw = function () {
  175. this._clearLastClusters()
  176. this._createClusters()
  177. }
  178. e.prototype.getGridSize = function () {
  179. return this._gridSize
  180. }
  181. e.prototype.setGridSize = function (h) {
  182. this._gridSize = h
  183. this._redraw()
  184. }
  185. e.prototype.getMaxZoom = function () {
  186. return this._maxZoom
  187. }
  188. e.prototype.setMaxZoom = function (h) {
  189. this._maxZoom = h
  190. this._redraw()
  191. }
  192. e.prototype.getStyles = function () {
  193. return this._styles
  194. }
  195. e.prototype.setStyles = function (h) {
  196. this._styles = h
  197. this._redraw()
  198. }
  199. e.prototype.getMinClusterSize = function () {
  200. return this._minClusterSize
  201. }
  202. e.prototype.setMinClusterSize = function (h) {
  203. this._minClusterSize = h
  204. this._redraw()
  205. }
  206. e.prototype.isAverageCenter = function () {
  207. return this._isAverageCenter
  208. }
  209. e.prototype.getMap = function () {
  210. return this._map
  211. }
  212. e.prototype.getMarkers = function () {
  213. return this._markers
  214. }
  215. e.prototype.getClustersCount = function () {
  216. var k = 0
  217. for (var j = 0, h; (h = this._clusters[j]); j++) {
  218. h.isReal() && k++
  219. }
  220. return k
  221. }
  222. function g(h) {
  223. this._markerClusterer = h
  224. this._map = h.getMap()
  225. this._minClusterSize = h.getMinClusterSize()
  226. this._isAverageCenter = h.isAverageCenter()
  227. this._center = null
  228. this._markers = []
  229. this._gridBounds = null
  230. this._isReal = false
  231. this._clusterMarker = new BMapLib.TextIconOverlay(
  232. this._center,
  233. this._markers.length,
  234. {
  235. styles: this._markerClusterer.getStyles(),
  236. }
  237. )
  238. }
  239. g.prototype.addMarker = function (k) {
  240. if (this.isMarkerInCluster(k)) {
  241. return false
  242. }
  243. if (!this._center) {
  244. this._center = k.getPosition()
  245. this.updateGridBounds()
  246. } else {
  247. if (this._isAverageCenter) {
  248. var j = this._markers.length + 1
  249. var o = (this._center.lat * (j - 1) + k.getPosition().lat) / j
  250. var m = (this._center.lng * (j - 1) + k.getPosition().lng) / j
  251. this._center = new BMap.Point(m, o)
  252. this.updateGridBounds()
  253. }
  254. }
  255. k.isInCluster = true
  256. this._markers.push(k)
  257. var h = this._markers.length
  258. if (h < this._minClusterSize) {
  259. this._map.addOverlay(k)
  260. return true
  261. } else {
  262. if (h === this._minClusterSize) {
  263. for (var n = 0; n < h; n++) {
  264. this._markers[n].getMap() && this._map.removeOverlay(this._markers[n])
  265. }
  266. }
  267. }
  268. this._map.addOverlay(this._clusterMarker)
  269. this._isReal = true
  270. this.updateClusterMarker()
  271. return true
  272. }
  273. g.prototype.isMarkerInCluster = function (j) {
  274. if (this._markers.indexOf) {
  275. return this._markers.indexOf(j) != -1
  276. } else {
  277. for (var k = 0, h; (h = this._markers[k]); k++) {
  278. if (h === j) {
  279. return true
  280. }
  281. }
  282. }
  283. return false
  284. }
  285. g.prototype.isMarkerInClusterBounds = function (h) {
  286. return this._gridBounds.containsPoint(h.getPosition())
  287. }
  288. g.prototype.isReal = function () {
  289. return this._isReal
  290. }
  291. g.prototype.updateGridBounds = function () {
  292. var h = new BMap.Bounds(this._center, this._center)
  293. this._gridBounds = b(this._map, h, this._markerClusterer.getGridSize())
  294. }
  295. g.prototype.updateClusterMarker = function () {
  296. if (this._map.getZoom() > this._markerClusterer.getMaxZoom()) {
  297. this._clusterMarker && this._map.removeOverlay(this._clusterMarker)
  298. for (var l = 0, j; (j = this._markers[l]); l++) {
  299. this._map.addOverlay(j)
  300. }
  301. return
  302. }
  303. if (this._markers.length < this._minClusterSize) {
  304. this._clusterMarker.hide()
  305. return
  306. }
  307. this._clusterMarker.setPosition(this._center)
  308. this._clusterMarker.setText(this._markers.length)
  309. var k = this._map
  310. var h = this.getBounds()
  311. this._clusterMarker.addEventListener('click', function () {
  312. k.setViewport(h)
  313. })
  314. }
  315. g.prototype.remove = function () {
  316. // eslint-disable-next-line
  317. for (var j = 0, h; (h = this._markers[j]); j++) {
  318. this._markers[j].getMap() && this._map.removeOverlay(this._markers[j])
  319. }
  320. this._map.removeOverlay(this._clusterMarker)
  321. this._markers.length = 0
  322. delete this._markers
  323. }
  324. g.prototype.getBounds = function () {
  325. var k = new BMap.Bounds(this._center, this._center)
  326. for (var j = 0, h; (h = this._markers[j]); j++) {
  327. k.extend(h.getPosition())
  328. }
  329. return k
  330. }
  331. g.prototype.getCenter = function () {
  332. return this._center
  333. }
  334. })()