maphandle.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {uuid} from '@/utils';
  2. export default {
  3. data() {
  4. return {
  5. polyLayer: null,
  6. layerId: 'vl',
  7. };
  8. },
  9. mounted() {
  10. this.layerId = uuid();
  11. if (!this.polyLayer) {
  12. this.polyLayer = new BDLayers.Lib.Layer.CBVectorLayer(
  13. `poly_layer${this.layerId}`, {enableAltitude: true});
  14. window.map.addCustomLayers(this.polyLayer, 1);
  15. }
  16. },
  17. methods: {
  18. drawPoly({
  19. name = '多边形',
  20. coordinates,
  21. altitude = 0,
  22. symbol = {},
  23. bizAttr = {},
  24. labelSymbol = {},
  25. polyOnClick = () => {
  26. },
  27. }, polyLayer) {
  28. const polygonId = `auto_poly_id${uuid()}`;
  29. let symbolParam = {
  30. lineColor: '#34495e',
  31. lineWidth: 2,
  32. polygonFill: '#1bbc9b',
  33. polygonOpacity: 0.4,
  34. };
  35. let labelSymbolParam = {
  36. labelText: name,
  37. labelTextColor: '#fefefe',
  38. labelTextSize: 15,
  39. };
  40. Object.assign(labelSymbolParam, labelSymbol);
  41. Object.assign(symbolParam, symbol);
  42. const polygon = new BDLayers.Lib.Overlays.Polygon('p1', {
  43. coordinates: coordinates,
  44. altitude,
  45. symbol: symbolParam,
  46. labelSymbol: labelSymbolParam,
  47. bizAttr: {...bizAttr, polygonId},
  48. });
  49. //多边形的点击事件
  50. polygon.on('click', polyOnClick);
  51. if (!polyLayer) {
  52. this.polyLayer.addGeometry(polygon);
  53. } else {
  54. polyLayer.addGeometry(polygon);
  55. }
  56. return polygon;
  57. },
  58. polygonToCoordinates(polygon) {
  59. // 正则表达式匹配坐标点
  60. const regex = /(-?\d+(\.\d+)?\s-?\d+(\.\d+)?)/g;
  61. const matches = polygon.match(regex);
  62. if (matches && matches.length > 0) {
  63. return matches
  64. .map(point => {
  65. const [x, y] = point.trim().split(' ').map(Number);
  66. return [x, y];
  67. });
  68. }
  69. return [];
  70. },
  71. createLayer(mapIns) {
  72. const uid = uuid();
  73. const layerId = `poly_layer${uid}`;
  74. const polyLayer = new BDLayers.Lib.Layer.CBVectorLayer(
  75. layerId, {enableAltitude: true});
  76. polyLayer.cust = {
  77. layerId: uid,
  78. mapIns,
  79. clearLayer: (layer) => {
  80. mapIns.removeLayersById(layerId);
  81. }
  82. }
  83. mapIns.addCustomLayers(polyLayer, 1);
  84. return polyLayer;
  85. },
  86. clearLayer(layer) {
  87. if (layer) {
  88. layer.removeAll();
  89. return;
  90. }
  91. this.polyLayer && this.polyLayer.removeAll();
  92. },
  93. destroyLayer(layerId, mapIns) {
  94. layerId && mapIns.removeLayersById(layerId)
  95. },
  96. },
  97. beforeDestroy() {
  98. this.polyLayer && window.map.removeLayersById(`poly_layer${this.layerId}`);
  99. },
  100. };