clusterDisplay.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // import * as ol from 'ol'
  2. import ol from './ol'
  3. class createCluster {
  4. constructor (option, map) {
  5. this.layer = null
  6. this.flag = false // 用于判断是否有目标图层
  7. let layer = map.getLayers()
  8. this.map = map
  9. this.imgUrl = option.imgUrl
  10. this.layerName = option.layerName // 传入聚合的目标图层的名字
  11. this.distance = option.distance || 10
  12. for (let i = 0; i < layer.array_.length; i++) {
  13. if (layer.array_[i].className_ === this.layerName) {
  14. this.flag = true
  15. this.layer = layer.array_[i]
  16. }
  17. }
  18. }
  19. openCluster () {
  20. if (this.flag) {
  21. let scale = 0.13
  22. let features = this.layer.getSource().getFeatures()
  23. var source = new ol.source.Vector({
  24. features: features
  25. })
  26. var clusterSource = new ol.source.Cluster({
  27. distance: this.distance,
  28. source: source
  29. })
  30. let style = new ol.style.Style({
  31. image: new ol.style.Icon({
  32. src: this.imgUrl,
  33. anchor: [0.5, 0.5],
  34. scale: scale
  35. })
  36. })
  37. this.layer.setSource(clusterSource)
  38. this.layer.setStyle(style)
  39. }
  40. }
  41. }
  42. function openNoticeLayerCluster (noticeImgUrl, map) {
  43. let cluster = new createCluster ({
  44. 'layerName': 'noticeLayer',
  45. 'imgUrl': noticeImgUrl
  46. }, map)
  47. cluster.openCluster()
  48. }
  49. function openReportLayerCluster (reportImgUrl, map) {
  50. let cluster = new createCluster ({
  51. 'layerName': 'reportLayer',
  52. 'imgUrl': reportImgUrl
  53. }, map)
  54. cluster.openCluster()
  55. }
  56. function openCultureLayerCluster (cultureImgUrl, map) {
  57. let cluster = new createCluster({
  58. 'layerName': 'cultureLayer',
  59. 'imgUrl': cultureImgUrl
  60. }, map)
  61. cluster.openCluster()
  62. }
  63. function openOtherLayerCluster (otherImgUrl, map) {
  64. let cluster = new createCluster ({
  65. 'layerName': 'otherLayer',
  66. 'imgUrl': otherImgUrl
  67. }, map)
  68. cluster.openCluster()
  69. }
  70. export default {openNoticeLayerCluster, openReportLayerCluster, openCultureLayerCluster, openOtherLayerCluster}