getPoint_Map.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <!--地图取点(获取经纬度)-->
  3. <el-dialog :visible.sync="dialogVisible" :title="title" size="tiny" :showClose="false" @close="cancel">
  4. <div class="mapStyle">
  5. <div class="mapLeftStyle">
  6. <el-input v-model="lon" placeholder="请输入经度" class="inputDUStyle" />
  7. <el-input v-model="lat" placeholder="请输入纬度" class="inputDUStyle" />
  8. <el-autocomplete
  9. style="width:100%;"
  10. popper-class="autoAddressClass"
  11. :popper-append-to-body="false"
  12. v-model="search"
  13. :fetch-suggestions="querySearchAsync"
  14. :trigger-on-focus="false"
  15. placeholder="详细地址"
  16. @select="handleSelect"
  17. clearable>
  18. <template slot-scope="{ item }">
  19. <i class="el-icon-location fl mgr10" style="margin-top: 10px;"></i>
  20. <div style="overflow:hidden;">
  21. <div class="title">{{ item.title }}</div>
  22. <span class="address ellipsis">{{ item.address }}</span>
  23. </div>
  24. </template>
  25. </el-autocomplete>
  26. </div>
  27. <div class="mapRightStyle">
  28. <baidu-map
  29. ak="7vzd6NZ0eTMsK6xxlv13MVcnThGYIG3q"
  30. scroll-wheel-zoom
  31. @ready="mapReady"
  32. >
  33. <!--地图视图-->
  34. <bm-view class="map"></bm-view>
  35. <!--显示更多-->
  36. <!-- <div class="more_panel">-->
  37. <!-- <span :class="{ down: isShowPanel }" @click.stop="showPanel">-->
  38. <!-- <span>{{ isShowPanel ? "隐藏" : "显示" }}</span-->
  39. <!-- >搜索列表<i class="el-icon-d-arrow-right"></i>-->
  40. <!-- </span>-->
  41. <!-- </div>-->
  42. <!--搜索-->
  43. <bm-local-search
  44. :keyword="keyword"
  45. :panel="isShowPanel"
  46. auto-viewport
  47. :zoom="10"
  48. style="display: none;"
  49. :location="location"
  50. ></bm-local-search>
  51. <!--点标注-->
  52. <bm-navigation anchor="BMAP_ANCHOR_TOP_LEFT" />
  53. <bm-marker-clusterer averageCenter>
  54. <bm-marker :position="location" />
  55. </bm-marker-clusterer>
  56. </baidu-map>
  57. </div>
  58. </div>
  59. <div slot="footer" class="dialog-footer">
  60. <el-button type="primary" @click="sure">确 定</el-button>
  61. <el-button @click="cancel">取 消</el-button>
  62. </div>
  63. </el-dialog>
  64. </template>
  65. <script>
  66. import BmInfoWindow from "vue-baidu-map/components/overlays/InfoWindow"; //标注弹窗
  67. import BaiduMap from "vue-baidu-map/components/map/Map.vue";
  68. import BmView from "vue-baidu-map/components/map/MapView.vue";
  69. import BmNavigation from "vue-baidu-map/components/controls/Navigation";
  70. import BmMarkerClusterer from "vue-baidu-map/components/extra/MarkerClusterer";
  71. import BmMarker from "vue-baidu-map/components/overlays/Marker";
  72. import BmLocalSearch from "vue-baidu-map/components/search/LocalSearch.vue";
  73. let geocoder;
  74. export default {
  75. name: 'getPoint_Map',
  76. props:{
  77. dialogVisible:Boolean,
  78. title:String,
  79. },
  80. components: {
  81. BaiduMap,
  82. BmView,
  83. BmNavigation,
  84. BmMarkerClusterer,
  85. BmMarker,
  86. BmLocalSearch,
  87. BmInfoWindow,
  88. },
  89. data(){
  90. return{
  91. lon:"",
  92. lat:"",
  93. search:"",
  94. keyword:"",
  95. map:{},
  96. BMap:{},
  97. mk:"",
  98. isShowPanel: true,
  99. location: {
  100. lng: 104.081534,
  101. lat: 30.655822
  102. },
  103. }
  104. },
  105. methods:{
  106. mapfrom(m){
  107. this.lon= m.lng
  108. this.lat= m.lat
  109. this.search= m.address
  110. this.keyword=m.address
  111. },
  112. mapReady({ BMap, map }) {
  113. this.BMap = BMap;
  114. this.map = map;
  115. geocoder = new BMap.Geocoder(); //创建地址解析器的实例
  116. if(this.search != ''){
  117. this.keyword = this.search
  118. }else{
  119. this.map.centerAndZoom(new BMap.Point(104.081534,30.655822), 16);
  120. }
  121. map.addEventListener("click", ({ point }) => {
  122. this.location.lng = point.lng;
  123. this.location.lat = point.lat;
  124. geocoder.getLocation(point, res => {
  125. this.search = res.address;
  126. this.lon = point.lng
  127. this.lat = point.lat
  128. this.$forceUpdate();
  129. });
  130. });
  131. },
  132. querySearchAsync(str,cb){
  133. var options = {
  134. onSearchComplete: function(res){ //检索完成后的回调函数
  135. var s = [];
  136. if (local.getStatus() == BMAP_STATUS_SUCCESS){
  137. for (var i = 0; i < res.getCurrentNumPois(); i ++){
  138. s.push(res.getPoi(i));
  139. }
  140. cb(s) //获取到数据时,通过回调函数cb返回到<el-autocomplete>组件中进行显示
  141. } else{
  142. cb(s)
  143. }
  144. }
  145. }
  146. var local = new BMap.LocalSearch(this.map, options) //创建LocalSearch构造函数
  147. local.search(str) //调用search方法,根据检索词str发起检索
  148. },
  149. handleSelect(item) {
  150. console.log(item.address);
  151. console.log(item.title);
  152. this.search = item.address + item.title; //记录详细地址,含建筑物名
  153. this.lon = item.point.lng
  154. this.lat = item.point.lat//记录当前选中地址坐标
  155. this.map.clearOverlays() //清除地图上所有覆盖物
  156. this.mk = new BMap.Marker(item.point) //根据所选坐标重新创建Marker
  157. this.map.addOverlay(this.mk) //将覆盖物重新添加到地图中
  158. this.map.panTo(item.point) //将地图的中心点更改为选定坐标点
  159. },
  160. cancel(){
  161. this.$emit('cancel')
  162. },
  163. sure(){
  164. this.$emit('sure',{lon:this.lon,lat:this.lat,search:this.search})
  165. },
  166. },
  167. }
  168. </script>
  169. <style lang="scss" scoped>
  170. .el-autocomplete-suggestion li{
  171. .title{
  172. font-size: 15px;
  173. color: #000;
  174. }
  175. .address{
  176. white-space: break-spaces;
  177. font-size: 12px;
  178. }
  179. }
  180. .mapStyle{
  181. width: 100%;
  182. .mapLeftStyle{
  183. width: 30%;
  184. margin-right: 5px;
  185. display: inline-block;
  186. .inputDUStyle{
  187. display: inline-block;
  188. width: 47%;
  189. }
  190. .inputDUStyle:first-child{
  191. margin-right: 1rem;
  192. }
  193. .inputDUStyle{
  194. margin-bottom: 1rem;
  195. }
  196. }
  197. .mapRightStyle{
  198. width: 68%;
  199. display: inline-block;
  200. vertical-align: top;
  201. .map {
  202. width: 100%;
  203. height: 500px;
  204. }
  205. }
  206. }
  207. </style>