index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. import { mapAddress } from './index';
  21. export default {
  22. data() {
  23. return {
  24. map: '',
  25. placeSearch: '',
  26. marker: '',
  27. addressName: '', //地址查询名称
  28. poisArray: [],
  29. };
  30. },
  31. props: {
  32. mapPopUpStatus: {
  33. type: Boolean,
  34. required: true
  35. }
  36. },
  37. computed: {},
  38. methods: {
  39. init() {
  40. let _this = this;
  41. _this.map = new AMap.Map('container', {
  42. resizeEnable: true, //是否监控地图容器尺寸变化
  43. zoom: 11 //初始化地图层级
  44. });
  45. // _this.map.on('click', _this.mapClick);
  46. _this.placeSearch = new AMap.PlaceSearch({
  47. map: _this.map
  48. });
  49. },
  50. mapClick(e) {
  51. if (this.marker !== '') {
  52. this.map.remove(this.marker);
  53. }
  54. let loc = [e.location.lat, e.location.lng];
  55. this.marker = new AMap.Marker({
  56. icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png',
  57. position: loc
  58. });
  59. this.map.add(this.marker);
  60. this.$emit('getDot',e)
  61. },
  62. togglePlaceSearch(val, resolve) {
  63. this.placeSearch.search(val, (status, result) => {
  64. if (result.info == 'OK') {
  65. this.poisArray = result.poiList.pois;
  66. }
  67. resolve && resolve(true);
  68. });
  69. },
  70. handleSelect(item) {
  71. this.mapClick(item);
  72. // this.$emit('getDot', item);
  73. },
  74. querySearch(queryString, cb) {
  75. new Promise((resolve) => {
  76. this.togglePlaceSearch(queryString, resolve);
  77. }).then(() => {
  78. if (this.poisArray.length !== 0) {
  79. cb(this.poisArray);
  80. }
  81. });
  82. }
  83. },
  84. created() {
  85. new Promise((resolve) => {
  86. if (!!AMap) {
  87. resolve(true);
  88. }
  89. }).then(() => {
  90. this.init();
  91. });
  92. }
  93. };
  94. </script>
  95. <style scoped lang="scss">
  96. /* --------------------------------高德地图样式----------------------------- */
  97. #container {
  98. margin-top: 20px;
  99. width: 100%;
  100. height: 400px;
  101. }
  102. .my-autocomplete {
  103. li {
  104. line-height: normal;
  105. padding: 7px;
  106. .name {
  107. text-overflow: ellipsis;
  108. overflow: hidden;
  109. span {
  110. font-size: 12px;
  111. color: #b4b4b4;
  112. }
  113. }
  114. }
  115. }
  116. </style>