Label.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import Common from './Common'
  2. import Feature from 'ol/Feature'
  3. import Point from 'ol/geom/Point'
  4. import Style from 'ol/style/Style'
  5. import Fill from 'ol/style/Fill'
  6. import Stroke from 'ol/style/Stroke'
  7. import Text from 'ol/style/Text'
  8. import KBaseObject from './KBaseObject'
  9. import * as proj from 'ol/proj'
  10. /**
  11. * @description KMap.Label 文本标记类
  12. */
  13. class Label extends KBaseObject{
  14. /**
  15. * Creates an instance of Label.
  16. * @param {*} param
  17. * @param {KMap.Map} [mapInstance=null] map对象,单地图的时候可不传,多地图时候需要传
  18. * @memberof Label
  19. */
  20. constructor(param,options,mapInstance = null){
  21. super(mapInstance)
  22. const vm = this
  23. let lng = (param.lng != undefined)? Number(param.lng) : vm.mapInstance.getCenter()[0]
  24. let lat = (param.lat != undefined)? Number(param.lat) : vm.mapInstance.getCenter()[1]
  25. let text = (param.text != undefined)? param.text : ""
  26. let font = (param.font != undefined)? param.font : '13px sans-serif'
  27. let offsetX = (param.offsetX != undefined)? param.offsetX : 0
  28. let offsetY = (param.offsetY != undefined)? param.offsetY : 0
  29. let rotation = (param.rotation != undefined)? param.rotation : 0
  30. let fill = (param.fill != undefined)? param.fill : "black"
  31. let backgroundColor = (param.backgroundColor != undefined)? param.backgroundColor : "#fff"
  32. let backgroundStroke = (param.backgroundStroke != undefined)? param.backgroundStroke : "black"
  33. let padding = (param.padding != undefined)? param.padding : [0,5,0,5]
  34. Common.checkLngLat(lng,lat)
  35. //创建文本标记
  36. vm.label = new Feature({
  37. geometry: new Point(proj.fromLonLat([lng,lat])),
  38. properties: null
  39. })
  40. vm.style = new Style({
  41. text: new Text({
  42. text: text,
  43. font: font,
  44. offsetX: offsetX,
  45. offsetY: offsetY,
  46. rotateWithView: false,//文本是否可旋转
  47. rotation: rotation,
  48. fill: new Fill({
  49. color: fill
  50. }),
  51. backgroundFill: new Fill({
  52. color: backgroundColor
  53. }),
  54. backgroundStroke: new Stroke({
  55. color: backgroundStroke
  56. }),
  57. padding: padding
  58. }),
  59. zIndex: 0
  60. })
  61. vm.label.setStyle(vm.style)
  62. vm.text = vm.style.getText() //文本标记内容
  63. vm.point = vm.label.getGeometry() //文本标记对象
  64. vm.source = vm.mapInstance.labelLayer.getSource() //地图文本标记图层
  65. if(options && options.source){
  66. vm.source = source;
  67. }
  68. //文本标记添加到地图
  69. vm.source.addFeature(vm.label)
  70. }
  71. /**
  72. * 获取id
  73. * @returns id
  74. */
  75. getId() {
  76. const vm = this
  77. let id = vm.label.getId()
  78. return id
  79. }
  80. /**
  81. * 设置id
  82. * @param id 必填
  83. */
  84. setId(id) {
  85. const vm = this
  86. vm.label.setId(id)
  87. }
  88. /**
  89. * 显示文本标记
  90. */
  91. show() {
  92. const vm = this
  93. if(!vm.source.hasFeature(label)) {
  94. vm.source.addFeature(label)
  95. }
  96. }
  97. /**
  98. * 隐藏文本标记
  99. */
  100. hide() {
  101. const vm = this
  102. if(vm.source.hasFeature(label)) {
  103. vm.source.removeFeature(label)
  104. }
  105. }
  106. /**
  107. * 删除文本标记
  108. */
  109. remove() {
  110. const vm = this
  111. if(vm.source.hasFeature(label)) {
  112. vm.source.removeFeature(label)
  113. }
  114. }
  115. /**
  116. * 获取文本标记坐标
  117. * @param 返回KMap.LngLat格式的经纬度
  118. */
  119. getPosition() {
  120. const vm = this
  121. let position = new proj.toLonLat(vm.point.getCoordinates())
  122. position = Common.MapLngLat2KMapLngLat(position)
  123. return position
  124. }
  125. /**
  126. * 设置文本标记坐标
  127. * @param KMap.LngLat格式的文本标记经纬度坐标,必填
  128. */
  129. setPosition(lnglat){
  130. lnglat = Common.KMapLngLat2MapLngLat(lnglat)
  131. let position = proj.fromLonLat(lnglat)
  132. point.setCoordinates(position)
  133. }
  134. /**
  135. * 获取文本标记缩放值
  136. * @returns 文本标记缩放值
  137. */
  138. getScale(){
  139. const vm = this
  140. let scale = vm.text.getScale()
  141. return scale
  142. }
  143. /**
  144. * 设置文本标记缩放值
  145. * @param scale 缩放值,必填
  146. */
  147. setScale(scale) {
  148. const vm = this
  149. vm.text.setScale(scale)
  150. }
  151. /**
  152. * 获取文本标记X轴方向偏移量
  153. * @returns X轴方向偏移量
  154. */
  155. getOffsetX(){
  156. const vm = this
  157. let offsetX = vm.text.offsetX_
  158. return offsetX
  159. }
  160. /**
  161. * 设置文本标记X轴方向偏移
  162. * @param X轴方向偏移(正向右,负向左),必填
  163. */
  164. setOffsetX(offsetX) {
  165. const vm = this
  166. vm.text.offsetX_ = offsetX
  167. }
  168. /**
  169. * 获取文本标记Y轴方向偏移量
  170. * @returns Y轴方向偏移量
  171. */
  172. getOffsetY() {
  173. const vm = this
  174. let offsetY = vm.text.offsetY_
  175. return offsetY
  176. }
  177. /**
  178. * 设置文本标记Y轴方向偏移
  179. * @param offsetY Y轴方向偏移(正向下,负向上),必填
  180. */
  181. setOffsetY(offsetY) {
  182. const vm = this
  183. vm.text.offsetY_ = offsetY
  184. }
  185. /**
  186. * 获取文本标记旋转弧度
  187. * @returns 文本标记旋转弧度
  188. */
  189. getAngle(){
  190. const vm = this
  191. let rotation = vm.text.rotation_
  192. return rotation
  193. }
  194. /**
  195. * 设置文本标记旋转弧度
  196. * @param rotation 文本标记旋转弧度,必填
  197. */
  198. setAngle(rotation){
  199. const vm = this
  200. vm.text.rotation_ = rotation
  201. }
  202. /**
  203. * 获取文本标记叠加顺序
  204. * @returns 文本标记叠加顺序
  205. */
  206. getZIndex() {
  207. const vm = this
  208. let index = vm.style.getZIndex()
  209. return index
  210. }
  211. /**
  212. * 设置文本标记叠加顺序
  213. * @param index index值较大的点标记显示在上方 ,值相同时后创建的点标记显示在上方,必填
  214. */
  215. setZIndex(index) {
  216. const vm = this
  217. vm.style.setZIndex(index)
  218. }
  219. /**
  220. * 获取用户自定义属性
  221. * @returns 用户自定义属性(支持数字,字符串,json)
  222. */
  223. getExtData(){
  224. const vm = this
  225. let extData = vm.label.values_.properties
  226. return extData
  227. }
  228. /**
  229. * 设置用户自定义属性
  230. * @param extData 用户自定义属性(支持数字,字符串,json),必填
  231. */
  232. setExtData(extData){
  233. const vm = this
  234. extData = (extData)? extData :null
  235. if(extData != null) {
  236. vm.label.values_.properties = extData
  237. }
  238. }
  239. /**
  240. * 注册文本标记事件
  241. * @param eventName 鼠标事件
  242. * "click":鼠标点击事件 "mousemove":鼠标悬停事件
  243. * @param callback 选中文本标记时触发函数
  244. */
  245. on(eventName,callback) {
  246. const vm = this
  247. vm.label.on(eventName,function(e) {
  248. if(eventName == "mousemove"){
  249. if(vm.label.ol_uid != vm.mapInstance.InfowindowmoveUID)
  250. {
  251. vm.mapInstance.InfowindowmoveUID = vm.label.ol_uid
  252. callback(e)
  253. }
  254. }
  255. else{
  256. callback(e)
  257. }
  258. })
  259. }
  260. /**
  261. * 注销文本标记事件(暂无该方法)
  262. */
  263. off(eventName,handler) {}
  264. /**
  265. * 获取文本标记地图对象
  266. * @returns 地图对象
  267. */
  268. getMap() {
  269. const vm = this
  270. let map = vm.map
  271. return map
  272. }
  273. /**
  274. * 设置文本标记地图对象,传入null时,移除点标记,
  275. * 建议不使用此API,使用remove方法
  276. * @param map 传入null,移除点标记
  277. */
  278. setMap(map) {
  279. const vm = this
  280. map = (vm.map)? vm.map : null
  281. if(map == null) {
  282. if(vm.source.hasFeature(vm.abel)) {
  283. vm.source.removeFeature(vm.label)
  284. }
  285. }
  286. }
  287. }
  288. export default Label