Amap.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <div class="container">
  3. <div class="search-box">
  4. <el-input
  5. v-model="searchKey"
  6. @keyup.enter.native="searchByHand"
  7. placeholder="请输入内容"
  8. class="input-with-select"
  9. >
  10. <el-button slot="append" icon="el-icon-search" @click="searchByHand"></el-button>
  11. </el-input>
  12. <!-- <input v-model="searchKey" type="search" id="search" />
  13. <button @click="searchByHand">搜索</button>-->
  14. <div class="tip-box" id="searchTip"></div>
  15. </div>
  16. <!--
  17. amap-manager: 地图管理对象
  18. vid:地图容器节点的ID
  19. zooms: 地图显示的缩放级别范围,在PC上,默认范围[3,18],取值范围[3-18];在移动设备上,默认范围[3-19],取值范围[3-19]
  20. center: 地图中心点坐标值
  21. plugin:地图使用的插件
  22. events: 事件
  23. -->
  24. <el-amap
  25. class="amap-box"
  26. :amap-manager="amapManager"
  27. :vid="'amap-vue'"
  28. :zoom="zoom"
  29. :plugin="plugin"
  30. :center="center"
  31. :events="events"
  32. mapStyle="fresh"
  33. >
  34. <!-- 标记 -->
  35. <el-amap-marker v-for="(marker, index) in markers" :position="marker" :key="index"></el-amap-marker>
  36. </el-amap>
  37. </div>
  38. </template>
  39. <script>
  40. import { AMapManager, lazyAMapApiLoaderInstance } from "vue-amap";
  41. let amapManager = new AMapManager();
  42. export default {
  43. data() {
  44. let _this = this;
  45. return {
  46. address: null,
  47. searchKey: "",
  48. amapManager,
  49. markers: [],
  50. searchOption: {
  51. city: "全国",
  52. citylimit: true
  53. },
  54. center: [121.329402, 31.228667],
  55. zoom: 17,
  56. lng: 0,
  57. lat: 0,
  58. loaded: false,
  59. events: {
  60. init() {
  61. lazyAMapApiLoaderInstance.load().then(() => {
  62. _this.initSearch();
  63. });
  64. },
  65. // 点击获取地址的数据
  66. click(e) {
  67. _this.markers = [];
  68. let { lng, lat } = e.lnglat;
  69. _this.lng = lng;
  70. _this.lat = lat;
  71. _this.center = [lng, lat];
  72. _this.markers.push([lng, lat]);
  73. // 这里通过高德 SDK 完成。
  74. let geocoder = new AMap.Geocoder({
  75. radius: 1000,
  76. extensions: "all"
  77. });
  78. console.log(geocoder);
  79. geocoder.getAddress([lng, lat], function(status, result) {
  80. if (status === "complete" && result.info === "OK") {
  81. if (result && result.regeocode) {
  82. _this.address =
  83. result.regeocode.formattedAddress;
  84. _this.searchKey =
  85. result.regeocode.formattedAddress;
  86. _this.$nextTick();
  87. }
  88. }
  89. });
  90. }
  91. },
  92. // 一些工具插件
  93. plugin: [
  94. // {
  95. // pName: 'Geocoder',
  96. // events: {
  97. // init (o) {
  98. // console.log(o.getAddress())
  99. // }
  100. // }
  101. // },
  102. {
  103. // 定位
  104. pName: "Geolocation",
  105. events: {
  106. init(o) {
  107. // o是高德地图定位插件实例
  108. o.getCurrentPosition((status, result) => {
  109. console.log(result);
  110. if (result && result.position) {
  111. // 设置经度
  112. _this.lng = result.position.lng;
  113. // 设置维度
  114. _this.lat = result.position.lat;
  115. // 设置坐标
  116. _this.center = [_this.lng, _this.lat];
  117. _this.markers.push([_this.lng, _this.lat]);
  118. // load
  119. _this.loaded = true;
  120. // 页面渲染好后
  121. _this.$nextTick();
  122. }
  123. });
  124. }
  125. }
  126. },
  127. {
  128. // 工具栏
  129. pName: "ToolBar",
  130. events: {
  131. init(instance) {
  132. console.log(instance);
  133. }
  134. }
  135. },
  136. {
  137. // 鹰眼
  138. pName: "OverView",
  139. events: {
  140. init(instance) {
  141. console.log(instance);
  142. }
  143. }
  144. },
  145. {
  146. // 搜索
  147. pName: "PlaceSearch",
  148. events: {
  149. init(instance) {
  150. console.log(instance);
  151. }
  152. }
  153. }
  154. ]
  155. };
  156. },
  157. methods: {
  158. initSearch() {
  159. let vm = this;
  160. let map = this.amapManager.getMap();
  161. AMapUI.loadUI(
  162. [
  163. "overlay/SimpleMarker", //SimpleMarker
  164. "overlay/SimpleInfoWindow" //SimpleInfoWindow
  165. ],
  166. function(SimpleMarker, SimpleInfoWindow) {
  167. console.dir(SimpleMarker)
  168. //....引用加载的UI....
  169. }
  170. );
  171. },
  172. searchByHand() {
  173. console.log("666");
  174. // this.poiPicker.searchByKeyword(this.searchKey)
  175. }
  176. }
  177. };
  178. </script>
  179. <style lang="scss" scope>
  180. .container {
  181. width: 100%;
  182. height: 100%;
  183. .search-box {
  184. margin-bottom: 15px;
  185. }
  186. }
  187. </style>