VectorStyle.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import LTBaseObject from './KBaseObject'
  2. import XYZ from 'ol/source/XYZ'
  3. import Tile from 'ol/layer/Tile'
  4. import Common from './Common'
  5. import Style from "ol/style/Style";
  6. import Fill from "ol/style/Fill";
  7. import Stroke from "ol/style/Stroke";
  8. import Text from "ol/style/Text";
  9. import Icon from "ol/style/Icon";
  10. import Circle from "ol/style/Circle";
  11. import Photo from "ol-ext/style/Photo";
  12. import config from '../../api/config.js'
  13. const { base_img_url2 } = config
  14. /**
  15. * @description 样式类
  16. */
  17. class VectorStyle{
  18. constructor(){
  19. this.polymerImgLayerStyleCache = {}
  20. this.getPointTextStyleCache = {}
  21. this.resize = "?x-oss-process=image/resize,w_400";
  22. }
  23. getPolymerImgLayerStyle(feature){
  24. let that = this
  25. // let zoom = that.kmap.view.getZoom();
  26. // let r = getRadius(zoom);
  27. let r = 25;
  28. let img = feature.get("src");
  29. let count = feature.get("count");
  30. let k = img + r;
  31. if (img && img != "") {
  32. let style = that.polymerImgLayerStyleCache[k];
  33. if (!style) {
  34. style = new Style({
  35. image: new Photo({
  36. src: base_img_url2 + img + that.resize,
  37. radius: r,
  38. shadow: 0,
  39. crop: true,
  40. kind: "square",
  41. onload: function () {
  42. that.polymerImgLayer.layer.changed();
  43. },
  44. displacement: [0, 30 + 15],
  45. stroke: new Stroke({
  46. width: 3,
  47. color: "#fdfcfc",
  48. }),
  49. }),
  50. });
  51. that.polymerImgLayerStyleCache[k] = style;
  52. }
  53. return [style, that.getCount(count + "")];
  54. } else {
  55. return that.getPointSimpleStyle(10, "#00000000", "#00000000", 1);
  56. }
  57. }
  58. getLineStyle(fillColor, strokeColor, strokeWidth){
  59. let style = new Style({
  60. stroke: new Stroke({
  61. color: strokeColor,
  62. width: strokeWidth || 1,
  63. }),
  64. });
  65. return style;
  66. }
  67. getPolygonStyle(fillColor, strokeColor, strokeWidth){
  68. let style = new Style({
  69. fill: new Fill({
  70. color: fillColor
  71. }),
  72. stroke: new Stroke({
  73. color: strokeColor,
  74. width: strokeWidth || 1,
  75. }),
  76. });
  77. return style;
  78. }
  79. getPointStyle(src, scale, anchor, text, font, textColor){
  80. let textObj = null;
  81. text && (textObj = new Text({
  82. text:text,
  83. stroke: new Stroke({
  84. color: textColor,
  85. width: 1,
  86. }),
  87. fill: new Fill({
  88. color: textColor,
  89. }),
  90. font:font
  91. }))
  92. let style = new Style({
  93. image: new Icon({
  94. text:textObj,
  95. src:src,
  96. scale:scale,
  97. anchor:anchor,
  98. })
  99. });
  100. return style
  101. }
  102. /**
  103. *
  104. * @param radius 半径
  105. * @param fillColor 填充颜色
  106. * @param strokeColor 边框颜色
  107. * @param strokeWidth 边框宽度
  108. * @returns {Style}
  109. */
  110. getPointSimpleStyle(radius, fillColor, strokeColor, strokeWidth){
  111. let style = new Style({
  112. image: new Circle({
  113. radius: radius, // 半径
  114. stroke: new Stroke({ // 边界样式
  115. color: strokeColor, // 边界颜色
  116. width: strokeWidth // 边界宽度
  117. }),
  118. fill: new Fill({ // 填充样式
  119. color: fillColor // 填充颜色
  120. })
  121. })
  122. });
  123. return style
  124. }
  125. /**
  126. *
  127. * @param text
  128. * @param fillColor 填充颜色
  129. * @param strokeColor 边框颜色
  130. * @param strokeWidth 边框宽度
  131. * @returns {Style}
  132. */
  133. getPointTextStyle(text, fillColor, strokeColor, strokeWidth, fontSize){
  134. if(this.getPointTextStyleCache[text]){
  135. return this.getPointTextStyleCache[text]
  136. }
  137. let style = new Style({
  138. text: new Text({
  139. text:text,
  140. stroke: new Stroke({
  141. color: strokeColor,
  142. width: strokeWidth,
  143. }),
  144. fill: new Fill({
  145. color: fillColor,
  146. }),
  147. font: fontSize+"px sans-serif"
  148. }),
  149. });
  150. this.getPointTextStyleCache[text] = style
  151. return style
  152. }
  153. /**
  154. *
  155. * @param text
  156. * @returns {Style}
  157. */
  158. getCount(text){
  159. let style = new Style({
  160. text: new Text({
  161. text:text,
  162. stroke: new Stroke({
  163. color: "#ffffff",
  164. width: 1,
  165. }),
  166. fill: new Fill({
  167. color: "#ffffff",
  168. }),
  169. font: "24rpx sans-serif",
  170. backgroundFill: new Fill({
  171. color: "#0c0c0c",
  172. }),
  173. backgroundStroke: new Stroke({
  174. color: "#cccccc",
  175. width: 1,
  176. }),
  177. offsetX:20,
  178. offsetY:-70 ,
  179. padding:[4, 5, 0, 5]
  180. }),
  181. });
  182. return style
  183. }
  184. }
  185. export default VectorStyle