29fe9aa0477af97308bc359b95f0d3b2f50c228d.svn-base 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { Marker, Coordinate } from 'maptalks'
  2. import { mapGoto } from './statusManagement'
  3. function structText(args) {
  4. var point = new Marker(args.point,
  5. {
  6. properties: {
  7. altitude: args.top
  8. },
  9. visible: true,
  10. editable: true,
  11. cursor: 'pointer',
  12. shadowBlur: 0,
  13. shadowColor: 'black',
  14. draggable: false,
  15. dragShadow: false, // display a shadow during dragging
  16. drawOnAxis: null, // force dragging stick on a axis, can be: x, y
  17. symbol: {
  18. 'textFaceName': 'sans-serif',
  19. 'textName': args.name,
  20. 'textFill': '#000000',
  21. 'textHorizontalAlignment': 'center',
  22. 'textVerticalAlignment': 'top',
  23. 'textSize': 12
  24. }
  25. }
  26. );
  27. point.on('click', () => {
  28. mapGoto({ center: args.point, zoom: 19 })
  29. })
  30. point.__id = args.id
  31. return point
  32. }
  33. function addToAttr(args) {
  34. var point = new Coordinate(args.point),
  35. containerPoint = map.coordinateToContainerPoint(point).round();
  36. args.position = [
  37. Number(containerPoint.x),
  38. Number(containerPoint.y)
  39. ]
  40. }
  41. function checkIsAdd(texts, args) {
  42. addToAttr(args)
  43. let tMinX = args.position[0] - args.width / 2
  44. let tMaxX = args.position[0] + args.width / 2
  45. let tMinY = args.position[1]
  46. let tMaxY = args.position[1] + args.height
  47. let index = texts.findIndex(text => {
  48. let minX = text._struct_attribute.position[0] - text._struct_attribute.width / 2
  49. let minY = text._struct_attribute.position[1]
  50. let maxX = text._struct_attribute.position[0] + text._struct_attribute.width / 2
  51. let maxY = text._struct_attribute.position[1] + text._struct_attribute.height
  52. return !(
  53. (tMinX < minX && tMaxX < minX) ||
  54. (tMinX > maxX && tMaxX > maxX) ||
  55. (tMinY < minY && tMaxY < minY) ||
  56. (tMinY > maxY && tMaxY > maxY)
  57. )
  58. })
  59. return !~index
  60. }
  61. function addText(mesh, textjsons) {
  62. if (textjsons.length === 0) return;
  63. let children = threeLayer.getScene().children
  64. let texts = []
  65. let newTexts = []
  66. children.forEach(mesh => {
  67. mesh.texts && texts.push(...mesh.texts)
  68. })
  69. textjsons.forEach(item => {
  70. if (checkIsAdd(texts, item)) {
  71. let text = structText(item)
  72. newTexts.push(text)
  73. text._struct_attribute = item
  74. texts.push(text)
  75. }
  76. })
  77. vector.addGeometry(newTexts)
  78. mesh.texts = newTexts
  79. mesh.textjsons = textjsons
  80. }
  81. function referText() {
  82. let children = threeLayer.getScene().children
  83. let checkTexts = []
  84. children.forEach(mesh => {
  85. if (mesh.texts) {
  86. for (let i = 0; i < mesh.texts.length; i++) {
  87. let text = mesh.texts[i]
  88. if (checkIsAdd(checkTexts, text._struct_attribute)) {
  89. checkTexts.push(text)
  90. } else {
  91. vector.removeGeometry(text)
  92. mesh.texts.splice(i, 1)
  93. --i
  94. }
  95. }
  96. }
  97. if (mesh.textjsons) {
  98. mesh.textjsons.map(item => {
  99. let index = mesh.texts.findIndex(text => text.__id === item.id)
  100. if (!~index && checkIsAdd(checkTexts, item)) {
  101. let text = structText(item)
  102. checkTexts.push(text)
  103. text._struct_attribute = item
  104. vector.addGeometry(text)
  105. mesh.texts.push(text)
  106. }
  107. })
  108. }
  109. })
  110. }
  111. export { referText }
  112. export default addText