VectorLayer.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import Layer from 'ol/layer/Vector'
  2. import Source from 'ol/source/Vector'
  3. import Common from './Common'
  4. import Select from 'ol/interaction/Select.js';
  5. import {singleClick} from 'ol/events/condition'
  6. /**
  7. * @description KMap.VectorLayer 矢量图层
  8. */
  9. class VectorLayer {
  10. /**
  11. * @param {string} name 图层名称
  12. * @param {string} zIndex 图层层级
  13. */
  14. constructor(name,zIndex,options){
  15. let source = new Source({
  16. crossOrigin:'anonymous',
  17. })
  18. let ShowLevel = Common.ShowLevel
  19. let minZoom = ShowLevel[0]
  20. let maxZoom = ShowLevel[1]
  21. let style = null;
  22. if(options && options.source){
  23. source = options.source
  24. }
  25. if(options && options.minZoom){
  26. minZoom = options.minZoom
  27. }
  28. if(options && options.maxZoom){
  29. maxZoom = options.maxZoom
  30. }
  31. if(options && options.style){
  32. style = options.style
  33. }
  34. let layer = new Layer({
  35. source:source,
  36. zIndex:zIndex,
  37. minZoom:minZoom,
  38. maxZoom:maxZoom,
  39. })
  40. layer.set('name', name)
  41. if(style){
  42. layer.setStyle(style)
  43. }
  44. this.layer = layer
  45. this.source = source
  46. }
  47. setMaxZoom(maxZoom){
  48. this.layer.setMaxZoom(maxZoom);
  49. }
  50. setMinZoom(minZoom){
  51. this.layer.setMinZoom(minZoom);
  52. }
  53. addFeature(feature){
  54. this.source.addFeature(feature)
  55. }
  56. refresh(){
  57. this.source.refresh()
  58. }
  59. getFeatureById(id){
  60. return this.source.getFeatureById(id)
  61. }
  62. addSingleSelect(callback,map,style){
  63. let option = {
  64. condition:singleClick,
  65. layers:[this.layer],
  66. multi:false
  67. };
  68. if(style){
  69. option["style"] = style
  70. }
  71. this.singleSelect = new Select(option)
  72. this.singleSelect.on("select",callback)
  73. map.addInteraction(this.singleSelect)
  74. }
  75. addToggleSelect(callback,map,style){
  76. let option = {
  77. condition:singleClick,
  78. toggleCondition: singleClick,
  79. layers:[this.layer],
  80. multi:true
  81. };
  82. if(style){
  83. option["style"] = style
  84. }
  85. this.toggleSelect = new Select(option)
  86. this.toggleSelect.on("select",callback)
  87. map.addInteraction(this.toggleSelect)
  88. }
  89. }
  90. export default VectorLayer