map.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <el-dialog
  3. class="dialog-images-manager"
  4. title="选择坐标"
  5. :visible="visible"
  6. width="960px"
  7. @close="handle_cancel">
  8. <!-- dialog-body -->
  9. <div style="height:50vh;overflow:auto">
  10. <el-input id="tipinput" style="width: 80%;margin-bottom: 10px;margin-right: 10px" v-model.trim="keywords" placeholder="请输入关键字搜索地址">
  11. </el-input>
  12. <el-button style="width:90px;" type="primary" @click.stop="search">搜索</el-button>
  13. <div id="map-wrapper"></div>
  14. </div>
  15. <span slot="footer" class="dialog-footer">
  16. <el-button @click="handle_cancel">取 消</el-button>
  17. <el-button type="primary" @click="handle_confirm">确 定</el-button>
  18. </span>
  19. </el-dialog>
  20. </template>
  21. <script>
  22. // 拖拽
  23. export default {
  24. name: 'map-choose',
  25. data() {
  26. return {
  27. location: {
  28. address: '地址',
  29. lng: "",
  30. lat: "",
  31. city: "",
  32. },
  33. keywords: '',
  34. visible: false,
  35. placeSearch: null,
  36. map: null,
  37. };
  38. },
  39. watch: {},
  40. mounted() {
  41. },
  42. methods: {
  43. initMap() {
  44. let that = this;
  45. if(this.location.lng) {
  46. this.map = new AMap.Map('map-wrapper',{
  47. resizeEnable: true, //是否监控地图容器尺寸变化
  48. zoom: 15, //指定缩放级别
  49. viewMode: '3D',
  50. center: [this.location.lng, this.location.lat] //初始化地图中心点
  51. });
  52. }else {
  53. this.map = new AMap.Map('map-wrapper',{
  54. resizeEnable: true, //是否监控地图容器尺寸变化
  55. zoom: 10, //指定缩放级别
  56. viewMode: '3D',
  57. });
  58. }
  59. //经纬度转地址 逆地址解析
  60. var geocoder = new AMap.Geocoder({
  61. radius: 1000 //范围,默认:500
  62. });
  63. AMap.plugin([
  64. 'AMap.ControlBar',
  65. ], () => {
  66. // 添加 3D 罗盘控制
  67. this.map.addControl(new AMap.ControlBar());
  68. });
  69. // 创建一个 Marker 实例:
  70. var marker = new AMap.Marker({
  71. position: new AMap.LngLat(116.39, 39.9),
  72. // icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
  73. // 设置是否可以拖拽
  74. draggable: true,
  75. cursor: 'move',
  76. // 设置拖拽效果
  77. // raiseOnDrag: true
  78. // title: '北京'
  79. });
  80. // 将创建的点标记添加到已有的地图实例:
  81. this.map.add(marker);
  82. this.map.on('click', (ev)=> {
  83. // 触发事件的对象
  84. // var target = ev.target;
  85. // 触发事件的地理坐标,AMap.LngLat 类型
  86. // var lnglat = ev.lnglat;
  87. // 触发事件的像素坐标,AMap.Pixel 类型
  88. // var pixel = ev.pixel;
  89. // 触发事件类型
  90. // var type = ev.type;
  91. that.location.lng = ev.lnglat.lng;
  92. that.location.lat = ev.lnglat.lat;
  93. marker.setPosition([ev.lnglat.lng, ev.lnglat.lat]); //更新点标记位置
  94. geocoder.getAddress([ev.lnglat.lng, ev.lnglat.lat], function(status, result) {
  95. if (status === 'complete'&&result.regeocode) {
  96. // console.log(result.regeocode.addressComponent)
  97. that.location.city = result.regeocode.addressComponent.city || result.regeocode.addressComponent.province;
  98. that.location.address = result.regeocode.formattedAddress;
  99. // console.log(that.location,'click')
  100. }else{
  101. that.location.city = "";
  102. that.location.address = '';
  103. console.error('根据经纬度查询地址失败')
  104. }
  105. });
  106. // console.log(target,lnglat,pixel,type)
  107. });
  108. marker.on('dragend', showInfoM);
  109. function showInfoM(e) {
  110. // console.log(e,'您拖拽了marker!')
  111. that.location.lng = e.lnglat.lng;
  112. that.location.lat = e.lnglat.lat;
  113. geocoder.getAddress([e.lnglat.lng, e.lnglat.lat], function(status, result) {
  114. if (status === 'complete'&&result.regeocode) {
  115. that.location.city = result.regeocode.addressComponent.city || result.regeocode.addressComponent.province;
  116. that.location.address = result.regeocode.formattedAddress;
  117. // console.log(that.location,'dragend')
  118. }else{
  119. that.location.address = '';
  120. console.error('根据经纬度查询地址失败')
  121. }
  122. });
  123. }
  124. //输入提示
  125. var autoOptions = {
  126. input: "tipinput"
  127. };
  128. var auto = new AMap.Autocomplete(autoOptions);
  129. this.placeSearch = new AMap.PlaceSearch({
  130. map: this.map
  131. }); //构造地点查询类
  132. AMap.event.addListener(auto, "select", this.select);//注册监听,当选中某条记录时会触发
  133. AMap.event.addListener(this.placeSearch, 'markerClick', function (e) {
  134. that.map.remove(marker);
  135. // console.log(e.data,"e.data")
  136. that.location.lng = e.data.location.lng;
  137. that.location.lat = e.data.location.lat;
  138. that.location.city = e.data.cityname || e.data.pname;
  139. that.location.address = e.data.pname + e.data.cityname + e.data.adname + e.data.address;
  140. // console.log(that.location,'markerClick')
  141. })
  142. },
  143. select(e) {
  144. this.placeSearch.setCity(e.poi.adcode);
  145. this.placeSearch.search(e.poi.name); //关键字查询查询
  146. },
  147. search() {
  148. this.placeSearch.search(this.keywords, function(status, result) {
  149. // 搜索成功时,result即是对应的匹配数据
  150. // console.log(status,result)
  151. })
  152. },
  153. destroyMap() {
  154. this.map && this.map.destroy();
  155. },
  156. show (location) {
  157. this.visible = true;
  158. if(location) {
  159. this.location = location;
  160. }
  161. // 延迟1秒让弹窗初始化地图
  162. setTimeout(()=> {
  163. this.initMap();
  164. },1000)
  165. },
  166. /**
  167. * 弹窗按钮 - 确认
  168. */
  169. handle_confirm () {
  170. this.$emit('confirm', this.location);
  171. this.destroyMap();
  172. this.visible = false;
  173. },
  174. /**
  175. * 弹窗按钮 - 取消
  176. */
  177. handle_cancel () {
  178. this.destroyMap();
  179. this.visible = false;
  180. },
  181. },
  182. };
  183. </script>
  184. <style lang="scss" scoped>
  185. #map-wrapper{
  186. width: 100%;
  187. height: 45vh;
  188. }
  189. </style>
  190. <style>
  191. .amap-sug-result {
  192. z-index: 2500;
  193. }
  194. </style>