Map-equipment.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 img1=require("@/assets/img/antFill-alert.svg")
  68. var img2=require("@/assets/img/antFill-alert(2).svg")
  69. var content=`<div class='${classId} ${pos.name}'>
  70. <div class="icon2-left" ><img src='${img1}'/></div>
  71. <div class="icon2-right">
  72. <div class="corner2" ></div>
  73. <div class="corner2-top" >荆鹏软件园荆鹏软件园01</div>
  74. <div class="corner2-bottom" ><img class="img2" src='${img2}'/>正常</div>
  75. </div>
  76. </div>`;
  77. marker.setContent(content)
  78. marker.setzIndex(920);
  79. },
  80. addIcon(pos){
  81. let marker = new AMap.Marker({
  82. //content: content,//"<img src='"+this.chargerIcon+"' style='height: 40px;width: 40px' />",
  83. //icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
  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({marker:marker,info:pos});
  96. },
  97. updateIcon(obj){
  98. let index = this.iconList.findIndex(item => item.info.name == obj.name);
  99. console.log("updateIcon",index)
  100. for(var i in this.iconList){
  101. var marker=this.iconList[i].marker
  102. var pos=this.iconList[i].info
  103. this.iconTemp1(marker,pos)
  104. }
  105. var pos=this.iconList[index].info
  106. var marker =this.iconList[index].marker
  107. this.iconTemp2(marker,pos)
  108. },
  109. myEmit(type,ob1,obj){
  110. var _this=this;
  111. console.log("myEmit")
  112. AMap.event.addListener(ob1, 'click', function(e) {
  113. console.log("myEmit2",type,ob1,obj)
  114. _this.$emit('clickMap',{
  115. type:type,
  116. obj:obj
  117. })
  118. })
  119. },
  120. setMyStyle(s) {
  121. this.myStyle = s;
  122. },
  123. logMapInfo() {
  124. var posCenter = this.mapcharger.getCenter();
  125. // //'center'+JSON.stringify(posCenter));
  126. var limitBounds = this.mapcharger.getBounds();
  127. let pos = {
  128. center: posCenter,
  129. bounds: limitBounds
  130. };
  131. return pos;
  132. },
  133. init() {
  134. _self = this;
  135. var _this = this;
  136. MapLoader().then(AMap => {
  137. _this.AMap = AMap;
  138. _this.mapcharger && _this.mapcharger.destroy();
  139. _this.mapcharger = new AMap.Map("container", {
  140. resizeEnable: true,
  141. dragEnable: true,
  142. center: [_this.longitude, _this.latitude],
  143. zoom: 13
  144. });
  145. _this.mapcharger.setMapStyle('amap://styles/f9b17f73bb4576ab1894c29fe9d03c6c');
  146. _this.$emit('onload')
  147. //_this.addPosition();
  148. _this.listenMove();
  149. })
  150. },
  151. listenMove() {
  152. var _this = this;
  153. _this.mapcharger.on('movestart', mapMovestart);
  154. _this.mapcharger.on('mapmove', mapMove);
  155. _this.mapcharger.on('moveend', mapMoveend);
  156. },
  157. }
  158. }
  159. </script>
  160. <style scoped>
  161. /*去除下标*/
  162. /deep/.amap-logo {
  163. display: none !important;
  164. }
  165. /deep/.amap-copyright {
  166. opacity: 0;
  167. font-size: 1px;
  168. }
  169. </style>