clusterDisplay.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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//聚合的距离默认30个像素
  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. //聚合标注数据源
  27. var clusterSource = new ol.source.Cluster({
  28. distance: this.distance, //聚合的距离参数,即当标注间距离小于此值时进行聚合,单位是像素
  29. source: source //聚合的数据源,即矢量要素数据源对象
  30. });
  31. //加载聚合标注的矢量图层
  32. let style = new ol.style.Style({
  33. image: new ol.style.Icon({
  34. src: this.imgUrl,
  35. anchor: [0.5, 0.5],
  36. scale: scale,
  37. })
  38. })
  39. this.layer.setSource(clusterSource)
  40. this.layer.setStyle(style)
  41. }
  42. }
  43. }
  44. function openNoticeLayerCluster(noticeImgUrl,map) {
  45. let cluster = new createCluster({
  46. 'layerName' : 'noticeLayer',
  47. 'imgUrl' : noticeImgUrl
  48. },map)
  49. cluster.openCluster()
  50. }
  51. function openReportLayerCluster(reportImgUrl,map) {
  52. let cluster = new createCluster({
  53. 'layerName' : 'reportLayer',
  54. 'imgUrl' : reportImgUrl
  55. },map)
  56. cluster.openCluster()
  57. }
  58. function openCultureLayerCluster(cultureImgUrl,map) {
  59. let cluster = new createCluster({
  60. 'layerName' : 'cultureLayer',
  61. 'imgUrl' : cultureImgUrl
  62. },map)
  63. cluster.openCluster()
  64. }
  65. function openOtherLayerCluster(otherImgUrl,map) {
  66. let cluster = new createCluster({
  67. 'layerName' : 'otherLayer',
  68. 'imgUrl' : otherImgUrl
  69. },map)
  70. cluster.openCluster()
  71. }
  72. export default {openNoticeLayerCluster,openReportLayerCluster,openCultureLayerCluster,openOtherLayerCluster}