popMap.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <div>
  3. <el-autocomplete
  4. popper-class="my-autocomplete"
  5. v-model="addressName"
  6. :fetch-suggestions="querySearch"
  7. placeholder="请输入地址查找"
  8. @select="handleSelect"
  9. >
  10. <template slot-scope="{ item }">
  11. <div class="name">
  12. {{ item.name }} <span>({{ item.cityname + item.adname }})</span>
  13. </div>
  14. </template>
  15. </el-autocomplete>
  16. <div id="container"></div>
  17. </div>
  18. </template>
  19. <script>
  20. export default {
  21. props: ['params'],
  22. data() {
  23. return {
  24. map: '',
  25. placeSearch: '',
  26. marker: '',
  27. addressName: '', //地址查询名称
  28. poisArray: [],
  29. thisMapPot: {}
  30. };
  31. },
  32. methods: {
  33. init() {
  34. let _this = this;
  35. this.map = new AMap.Map('container', {
  36. resizeEnable: true, //是否监控地图容器尺寸变化
  37. zoom: 11 //初始化地图层级
  38. });
  39. this.map.on('complete', () => {
  40. if (!!_this.params.location) {
  41. let arr = this.params.location.split(',');
  42. this.addIcon({ lng: arr[0], lat: arr[1] });
  43. }
  44. });
  45. this.map.on('click', this.mapClick);
  46. this.placeSearch = new AMap.PlaceSearch({
  47. map: _this.map
  48. });
  49. AMap.event.addListener(this.placeSearch, 'markerClick', (e) => {
  50. this.thisMapPot = e.event.lnglat;
  51. this.$message.success('选择成功');
  52. });
  53. },
  54. mapClick(e) {
  55. this.addIcon(e.lnglat);
  56. this.$message.success('选择成功');
  57. },
  58. addIcon(e) {
  59. if (!!this.marker) {
  60. this.map.remove(this.marker);
  61. this.marker = '';
  62. }
  63. this.thisMapPot = e;
  64. let loc = [e.lng, e.lat];
  65. this.marker = new AMap.Marker({
  66. icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png',
  67. position: loc,
  68. map: this.map
  69. });
  70. this.marker.setLabel({
  71. offset: new AMap.Pixel(-50, 35),
  72. content: !!this.params.data.pointName ? `巡更名称:${this.params.data.pointName}` : `巡更经纬度:${e.lng},${e.lat}`
  73. });
  74. this.map.setFitView();
  75. },
  76. submit() {
  77. this.params.callback && this.params.callback(this.thisMapPot);
  78. this.$emit('close');
  79. },
  80. togglePlaceSearch(val, resolve) {
  81. this.placeSearch.search(val, (status, result) => {
  82. if (result.info == 'OK' && status == 'complete') {
  83. this.poisArray = result.poiList.pois;
  84. }
  85. resolve && resolve(true);
  86. });
  87. },
  88. handleSelect(item) {
  89. this.addressName = item.name;
  90. this.placeSearch.search(item.name);
  91. },
  92. querySearch(queryString, cb) {
  93. new Promise((resolve) => {
  94. this.togglePlaceSearch(queryString, resolve);
  95. }).then(() => {
  96. if (this.poisArray.length !== 0) {
  97. cb(this.poisArray);
  98. }
  99. });
  100. }
  101. },
  102. destroyed() {
  103. this.map && this.map.destroy();
  104. this.marker = '';
  105. },
  106. created() {
  107. this.$nextTick(() => {
  108. this.init();
  109. });
  110. }
  111. };
  112. </script>
  113. <style scoped lang="scss">
  114. /* --------------------------------高德地图样式----------------------------- */
  115. #container {
  116. margin-top: 20px;
  117. width: 100%;
  118. height: 400px;
  119. /deep/ .amap-marker-label {
  120. border: 0 none;
  121. background-color: #fff;
  122. white-space: nowrap;
  123. box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.3);
  124. border-radius: 5px;
  125. }
  126. // /deep/ .amap-marker-label:after {
  127. // position: absolute;
  128. // border: 5px solid transparent;
  129. // border-top-color: #fff;
  130. // top: 19px;
  131. // left: 43%;
  132. // content: '';
  133. // width: 0;
  134. // height: 0;
  135. // }
  136. }
  137. .my-autocomplete {
  138. li {
  139. line-height: normal;
  140. padding: 7px;
  141. .name {
  142. text-overflow: ellipsis;
  143. overflow: hidden;
  144. span {
  145. font-size: 12px;
  146. color: #b4b4b4;
  147. }
  148. }
  149. }
  150. }
  151. </style>