Map-equipment.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <view>
  3. <view id="container" :style="myStyle"></view>
  4. </view>
  5. </template>
  6. <script>
  7. let _self;
  8. function mapMovestart(e) {
  9. //'movestart')
  10. }
  11. function mapMove(e) {
  12. // //'mapMove')
  13. }
  14. function mapMoveend(e) {
  15. //let _self = e;
  16. let pos = _self.logMapInfo();
  17. _self.$emit('onMoveEnd', pos);
  18. //'mapMoveend')
  19. }
  20. import MapLoader from '@/apis/utils/AMap'
  21. export default {
  22. name: "Map-equipment",
  23. data() {
  24. return {
  25. AMap: null,
  26. mapcharger: null,
  27. myStyle: "width: 100%; height: 1200rpx;",
  28. longitude: 112.276527,
  29. latitude: 30.306427,
  30. iconList: []
  31. };
  32. },
  33. methods: {
  34. setList(sz) {
  35. if (this.AMap == null) {
  36. return
  37. }
  38. for (var i in this.iconList) {
  39. if (this.iconList[i].marker) {
  40. this.iconList[i].marker.setMap(null);
  41. }
  42. this.mapcharger.remove(this.iconList[i]);
  43. }
  44. this.iconList = []
  45. for (var i in sz) {
  46. var ob = sz[i];
  47. this.addIcon(ob);
  48. }
  49. },
  50. iconTemp1(marker, pos) {
  51. var classId = ""
  52. if (pos.status == 1) {
  53. classId = "location1"
  54. }
  55. if (pos.status == 2) {
  56. classId = "location2"
  57. }
  58. //初始化原点模板
  59. var img = require("@/assets/img/antFill-alert Copy 1.svg")
  60. var content = `<div class='${classId} ${pos.name}'><img src='${img}'/><div class="corner"></div></div>`;
  61. marker.setContent(content)
  62. marker.setzIndex(900);
  63. },
  64. iconTemp2(marker, pos) {
  65. //展开原点模板
  66. var classId = "location2"
  67. var statusName = pos.online ? '在线' : '离线';
  68. var img1 = require("@/assets/img/antFill-alert.svg")
  69. var img2 = require("@/assets/img/antFill-alert(2).svg")
  70. var content = `<div class='${classId} ${pos.name}'>
  71. <div class="icon2-left" ><img src='${img1}'/></div>
  72. <div class="icon2-right">
  73. <div class="corner2" ></div>
  74. <div class="corner2-top" >${pos.name}</div>
  75. <div class="corner2-bottom" ><img class="img2" src='${img2}'/>${statusName}</div>
  76. </div>
  77. </div>`;
  78. marker.setContent(content)
  79. marker.setzIndex(920);
  80. },
  81. addIcon(pos) {
  82. let marker = new AMap.Marker({
  83. content: '',
  84. position: [pos.longitude, pos.latitude],
  85. //offset: new AMap.Pixel(-20, -40),
  86. offset: new AMap.Pixel(0, 0), //设置点标记偏移量
  87. anchor: 'bottom-center',
  88. zIndex: 900,
  89. showPositionPoint: true,
  90. autoRotation: true,
  91. });
  92. this.iconTemp1(marker, pos)
  93. marker.setMap(this.mapcharger);
  94. this.myEmit("charger", marker, pos)
  95. this.iconList.push({
  96. marker: marker,
  97. info: pos
  98. });
  99. },
  100. updateIcon(obj) {
  101. if (this.iconList.length == 0) {
  102. return
  103. }
  104. let index = this.iconList.findIndex(item => item.info.id == obj.id);
  105. console.log("updateIcon", index)
  106. for (var i in this.iconList) {
  107. var marker = this.iconList[i].marker
  108. var pos = this.iconList[i].info
  109. this.iconTemp1(marker, pos)
  110. }
  111. var pos = this.iconList[index].info
  112. var marker = this.iconList[index].marker
  113. this.iconTemp2(marker, pos)
  114. },
  115. myEmit(type, ob1, obj) {
  116. var _this = this;
  117. console.log("myEmit")
  118. AMap.event.addListener(ob1, 'click', function(e) {
  119. console.log("myEmit2", type, ob1, obj)
  120. _this.$emit('clickMap', {
  121. type: type,
  122. obj: obj
  123. })
  124. })
  125. },
  126. setMyStyle(s) {
  127. this.myStyle = s;
  128. },
  129. logMapInfo() {
  130. var posCenter = this.mapcharger.getCenter();
  131. // //'center'+JSON.stringify(posCenter));
  132. var limitBounds = this.mapcharger.getBounds();
  133. let pos = {
  134. center: posCenter,
  135. bounds: limitBounds
  136. };
  137. return pos;
  138. },
  139. init() {
  140. _self = this;
  141. var _this = this;
  142. MapLoader().then(AMap => {
  143. this.AMap = AMap;
  144. _this.mapcharger && _this.mapcharger.destroy();
  145. _this.mapcharger = new AMap.Map("container", {
  146. resizeEnable: true,
  147. dragEnable: true,
  148. center: [this.longitude, this.latitude],
  149. zoom: 13
  150. });
  151. _this.mapcharger.setMapStyle('amap://styles/f9b17f73bb4576ab1894c29fe9d03c6c');
  152. _this.$emit('onload')
  153. //_this.addPosition();
  154. _this.listenMove();
  155. })
  156. },
  157. listenMove() {
  158. var _this = this;
  159. _this.mapcharger.on('movestart', mapMovestart);
  160. _this.mapcharger.on('mapmove', mapMove);
  161. _this.mapcharger.on('moveend', mapMoveend);
  162. },
  163. }
  164. }
  165. </script>
  166. <style scoped>
  167. /*去除下标*/
  168. ::v-deep.amap-logo {
  169. display: none !important;
  170. }
  171. ::v-deep.amap-copyright {
  172. opacity: 0;
  173. font-size: 1px;
  174. }
  175. </style>