Polygon.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import OLPolygon from 'ol/geom/Polygon'
  2. import Feature from 'ol/Feature'
  3. import Fill from 'ol/style/Fill'
  4. import Stroke from 'ol/style/Stroke'
  5. import Style from 'ol/style/Style'
  6. import * as proj from 'ol/proj'
  7. import * as olExtent from 'ol/extent'
  8. import KBaseObject from './KBaseObject'
  9. /**
  10. * @description KMap.Polygon 面标记类
  11. */
  12. class Polygon extends KBaseObject{
  13. /**
  14. * @param {*} positions
  15. * @param {*} style
  16. * @param {KMap.Map} [mapInstance=null] map对象,单地图的时候可不传,多地图时候需要传
  17. * @memberof Polygon
  18. */
  19. constructor(positions,style,options,mapInstance = null){
  20. super(mapInstance)
  21. const vm = this
  22. const layer = vm.mapInstance.polygonLayer
  23. vm.source = layer.getSource();
  24. vm.style = vm.initStyle(style)
  25. vm.polygon = vm.initFeature(positions,vm.style)
  26. vm.source.addFeature(vm.polygon)
  27. }
  28. initStyle(style){
  29. let fillColor = (style!=undefined && style.fillColor)?style.fillColor:'rgba(255,0,0,0.5)'
  30. let strokeColor = (style!=undefined && style.strokeColor)?style.strokeColor:'rgba(255,0,0,1)'
  31. let strokeWidth = (style!=undefined && style.strokeWidth)?style.strokeWidth:1
  32. let fill = new Fill({
  33. color: fillColor
  34. })
  35. let stroke = new Stroke({
  36. color: strokeColor,
  37. width: strokeWidth
  38. })
  39. let newStyle= new Style({
  40. fill: fill,
  41. stroke: stroke
  42. })
  43. return newStyle
  44. }
  45. initFeature(positions,style){
  46. let newPositions = []
  47. positions.forEach(function(position){
  48. if(position instanceof Array)
  49. newPositions.push(position)
  50. else{
  51. newPositions.push([position.getLng(),position.getLat()])
  52. }
  53. })
  54. let geometry = new OLPolygon([newPositions])
  55. geometry.applyTransform(proj.getTransform('EPSG:4326', 'EPSG:3857'))
  56. //创建面标记
  57. let polygon = new Feature({geometry:geometry})
  58. polygon.setStyle(style)
  59. return polygon
  60. }
  61. /**
  62. * 地图视角缩放到线标记范围
  63. * @param duration 动画持续时间(单位:毫秒) 选填,默认0毫秒
  64. */
  65. zoomToExtent(duration) {
  66. const vm = this
  67. duration = (duration)? duration : 0
  68. let extent = vm.polygon.getGeometry().getExtent()
  69. // let extentBound = olExtent.boundingExtent(LonLatArray)
  70. vm.map.getView().fit(extent,{
  71. duration: duration
  72. })
  73. }
  74. /**
  75. * @description 显示
  76. * @memberof Polygon
  77. */
  78. show(){
  79. const vm = this
  80. if(!vm.source.hasFeature(vm.polygon)) {
  81. vm.source.addFeature(vm.polygon)
  82. }
  83. }
  84. /**
  85. * @description 隐藏
  86. * @memberof Polygon
  87. */
  88. hide(){
  89. const vm = this
  90. if(vm.source.hasFeature(vm.polygon)) {
  91. vm.source.removeFeature(vm.polygon)
  92. }
  93. }
  94. /**
  95. * @description 移除
  96. * @memberof Polygon
  97. */
  98. remove(){
  99. const vm = this
  100. if(vm.source.hasFeature(vm.polygon)) {
  101. vm.source.removeFeature(vm.polygon)
  102. }
  103. }
  104. }
  105. export default Polygon