当前位置:   article > 正文

vue调用百度地图获取定位坐标(PC端)

vue调用百度地图获取定位坐标(PC端)

1.组件代码(BaiduMapWorld.vue),将该组件引入项目中。

  1. <template>
  2. <div>
  3. <el-dialog
  4. title="定位设置"
  5. :visible.sync="mapVisible"
  6. left
  7. :append-to-body="true"
  8. :lock-scroll="false"
  9. width="65%"
  10. :show-close="false"
  11. >
  12. <div>
  13. <el-select
  14. class="select"
  15. v-model="search"
  16. filterable
  17. remote
  18. clearable
  19. reserve-keyword
  20. @clear="clearSearch"
  21. placeholder="请输入地址"
  22. :remote-method="mapNameChange"
  23. @change="mapNameChange($event,'change')"
  24. :loading="loading"
  25. style="width: 100%;">
  26. <el-option
  27. v-for="item in searchOption"
  28. :key="item.title"
  29. :label="item.title"
  30. :value="item.title">
  31. </el-option>
  32. </el-select>
  33. </div>
  34. <baidu-map class="map" :center="center" :zoom="zoom" @ready="handler"
  35. :scroll-wheel-zoom="true"
  36. @click="clickEvent"
  37. ak="33B192o1jPaqOHASGGAIkoMuwi8W76j3">
  38. <!-- 地图控件位置 -->
  39. <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
  40. <!-- 城市列表 -->
  41. <bm-city-list anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list>
  42. <!-- 定位当前位置 -->
  43. <bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT" :showAddressBar="true" :autoLocation="true" @locationSuccess="getLoctionSuccess" ></bm-geolocation>
  44. <!-- 地图容器 -->
  45. <bm-view :style="{width:'100%',height: this.clientHeight+'px',flex: 1,marginBottom:'-30px'}"></bm-view>
  46. </baidu-map>
  47. <div class="demo-input-suffix" >
  48. <el-link type="info">lng:</el-link><el-input class="lineinput" style="width:100px" size="mini" v-model.number="locData.longitude"></el-input>
  49. <el-link type="info">lat:</el-link><el-input class="lineinput" style="width:100px" size="mini" v-model.number="locData.latitude"></el-input>
  50. <el-link type="info">地址:</el-link><el-input class="lineinput" style="width:250px" size="mini" v-model="locData.address"></el-input>
  51. </div>
  52. <div slot="footer" class="dialog-footer">
  53. <el-button type="warning" size="small" icon="el-icon-close" @click.native="close">取消</el-button>
  54. <el-button type="primary" size="small" icon="el-icon-check" @click.native="findlocation">保存</el-button>
  55. </div>
  56. </el-dialog>
  57. </div>
  58. </template>
  59. <script>
  60. let geoc = null;
  61. import {BaiduMap,BmNavigation,BmView,BmGeolocation,BmCityList} from 'vue-baidu-map'
  62. export default {
  63. name: "mapDialog",
  64. components: {
  65. BaiduMap,
  66. BmNavigation,
  67. BmView,
  68. BmGeolocation,
  69. BmCityList
  70. },
  71. props:{
  72. mapVisible:""
  73. },
  74. data () {
  75. return {
  76. center: {lng: 117.063035, lat: 36.672767},
  77. zoom: 12,
  78. locData:{
  79. longitude:'',
  80. latitude:'',
  81. address:'',
  82. },
  83. clientHeight:document.documentElement.clientHeight-450, // 屏幕高度
  84. iconUrl:'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1505732009,4176072429&fm=26&gp=0.jpg',
  85. search:'',
  86. searchOption:[],
  87. loading:false
  88. }
  89. },
  90. mounted() {
  91. },
  92. methods: {
  93. handler ({BMap, map}) {
  94. let _this = this; // 设置一个临时变量指向vue实例,因为在百度地图回调里使用this,指向的不是vue实例;
  95. var geolocation = new BMap.Geolocation();
  96. geolocation.getCurrentPosition(function(r){
  97. //console.log(r);
  98. _this.center = {lng: r.longitude, lat: r.latitude}; // 设置center属性值
  99. _this.autoLocationPoint = {lng: r.longitude, lat: r.latitude}; // 自定义覆盖物
  100. _this.initLocation = true;
  101. },{enableHighAccuracy: true})
  102. window.map = map;
  103. },
  104. //点击地图监听
  105. clickEvent(e){
  106. map.clearOverlays();
  107. let Icon_0 = new BMap.Icon("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1505732009,4176072429&fm=26&gp=0.jpg", new BMap.Size(64, 64), {anchor: new BMap.Size(18, 32),imageSize: new BMap.Size(36, 36)});
  108. var myMarker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat),{icon: Icon_0});
  109. map.addOverlay(myMarker);
  110. //用所定位的经纬度查找所在地省市街道等信息
  111. var point = new BMap.Point(e.point.lng, e.point.lat);
  112. var gc = new BMap.Geocoder();
  113. let _this = this;
  114. gc.getLocation(point, function (rs) {
  115. var addComp = rs.addressComponents;
  116. //console.log(rs.address);//地址信息
  117. _this.locData.address = rs.address;
  118. });
  119. this.locData.longitude = e.point.lng;
  120. this.locData.latitude = e.point.lat;
  121. },
  122. //定位成功回调
  123. getLoctionSuccess(point, AddressComponent, marker){
  124. map.clearOverlays();
  125. let Icon_0 = new BMap.Icon("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1505732009,4176072429&fm=26&gp=0.jpg", new BMap.Size(64, 64), {anchor: new BMap.Size(18, 32),imageSize: new BMap.Size(36, 36)});
  126. var myMarker = new BMap.Marker(new BMap.Point(point.point.lng, point.point.lat),{icon: Icon_0});
  127. map.addOverlay(myMarker);
  128. this.locData.longitude = point.point.lng;
  129. this.locData.latitude = point.point.lat;
  130. },
  131. mapNameChange(e,data) {
  132. this.loading = true;
  133. if(!e && !this.search){
  134. return false
  135. }
  136. let that = this,
  137. point,
  138. marker = null;
  139. geoc = new BMap.Geocoder();
  140. let local = new BMap.LocalSearch(map, {
  141. renderOptions: { map: map },
  142. onSearchComplete: res => {
  143. this.loading = false;
  144. // console.log(res);
  145. if(res){
  146. if(data!=="change"){
  147. that.searchOption = res.Kr
  148. }
  149. if(res.Kr.length>0){
  150. that.$set(that.locData,'longitude',res.Kr[0].point.lng)
  151. that.$set(that.locData,'latitude',res.Kr[0].point.lat)
  152. }
  153. }
  154. if (local.getResults() != undefined) {
  155. map.clearOverlays(); //清除地图上所有覆盖物
  156. if (local.getResults().getPoi(0)) {
  157. point = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
  158. map.centerAndZoom(point, 10);
  159. marker = new BMap.Marker(point); // 创建标注
  160. map.addOverlay(marker); // 将标注添加到地图中
  161. marker.enableDragging(); // 可拖拽
  162. geoc.getLocation(point, function(rs) {
  163. var addComp = rs.addressComponents;
  164. that.$set(that.locData,'address',addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber)
  165. });
  166. } else {
  167. this.$message('未匹配到地点!可拖动地图上的红色图标到精确位置');
  168. }
  169. } else {
  170. this.$message("未找到搜索结果");
  171. }
  172. }
  173. });
  174. local.search(e || this.search);
  175. },
  176. findlocation(){
  177. this.$emit("mapLocationSave",this.locData)
  178. },
  179. clearSearch(){
  180. this.loading = false
  181. },
  182. close(){
  183. this.$emit('mapLocationClose')
  184. }
  185. }
  186. }
  187. </script>
  188. <!-- Add "scoped" attribute to limit CSS to this component only -->
  189. <style>
  190. .map{
  191. margin-top: 5px;
  192. }
  193. .demo-input-suffix{
  194. width: 100%;
  195. margin-top: 40px;
  196. }
  197. .BMap_bubble_title a{ /* 隐藏搜索结果的详情按钮 */
  198. display: none;
  199. }
  200. .anchorBL{display:none} /* 去除百度地图左下角的介绍文字 */
  201. </style>

2.自己本地编辑页面调用(可根据自己需求修改样式,或抽成公共样式等,这里的样式随便弄的)

  1. <template>
  2. <div>
  3. <el-dialog :title="title" :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="1200px" top="8vh">
  4. <el-form className="xy-form" :model="form" ref="form" :disabled="isDisabled" labelWidth="155px">
  5. <!--地图定位-->
  6. <el-form-item label="工程坐标" :span="12">
  7. <el-col :span="7" style="padding-left: 0px;padding-right: 0px;">
  8. <el-input v-model="form.gpsX" :title="form.gpsX" :disabled="true"></el-input>
  9. </el-col>
  10. <el-col :span="3" style="padding-left: 0px;padding-right: 0px;text-align: center;">经度</el-col>
  11. <el-col :span="7" style="padding-left: 0px;padding-right: 0px;">
  12. <el-input v-model="form.gpsY" :title="form.gpsY" :disabled="true"></el-input>
  13. </el-col>
  14. <el-col :span="3" style="padding-left: 0px;padding-right: 0px;text-align: center;">纬度</el-col>
  15. <el-col :span="2" class="location"
  16. @click.native="selectPosition()">
  17. <img src="../img/location.png" alt="" />
  18. </el-col>
  19. </el-form-item>
  20. <el-form-item label="工程地址" prop="address" :span="12">
  21. <el-input v-model="form.address"></el-input>
  22. </el-form-item>
  23. </el-form>
  24. <div slot="footer" class="dialog-footer">
  25. <el-button @click="dialogFormVisible = false">关 闭</el-button>
  26. </div>
  27. <BaiduMapWorld
  28. :mapVisible="mapVisible"
  29. v-on:mapLocationClose="mapLocationClose"
  30. v-on:mapLocationSave="mapLocationSave"
  31. ></BaiduMapWorld>
  32. </el-dialog>
  33. </div>
  34. </template>
  35. <script>
  36. import BaiduMapWorld from "@/views/project/managerSection/components/BaiduMapWorld"
  37. export default {
  38. components: {
  39. BaiduMapWorld
  40. },
  41. data() {
  42. return {
  43. mapVisible: false,
  44. isDisabled :'',
  45. form: {
  46. id : '',
  47. gpsX : '',
  48. gpsY : '',
  49. address : '',
  50. },
  51. }
  52. },
  53. methods: {
  54. //获取百度地图定位坐标
  55. selectPosition() {
  56. this.mapVisible = true;
  57. },
  58. mapLocationClose() {
  59. this.mapVisible = false;
  60. },
  61. mapLocationSave(param) {
  62. this.mapVisible = false;
  63. this.form.gpsX = param.longitude;
  64. this.form.gpsY = param.latitude;
  65. this.form.address = param.address;
  66. },
  67. }
  68. }
  69. </script>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/109817
推荐阅读
相关标签
  

闽ICP备14008679号