xxx.vue

  1. <template>
  2. <div>
  3. <el-input v-model="inputaddr">
  4. </el-input>
  5. <el-button @click="mapCpm">点击</el-button>
  6. <el-dialog :modal-append-to-body="false"
  7. :title="textMap[dialogStatus]"
  8. :visible.sync="mapCPM">
  9. <baidu-map class="map"
  10. :center="center"
  11. :zoom="zoom"
  12. @ready="handler"
  13. :double-click-zoom='false'
  14. :scroll-wheel-zoom='true'>
  15. <bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
  16. :showAddressBar="true"
  17. :autoLocation="true"
  18. @locationSuccess='locationSuccess'></bm-geolocation>
  19. <bm-panorama></bm-panorama>
  20. <bm-marker :position="center"
  21. @dragend="dragend"
  22. :raiseOnDrag='true'
  23. :dragging='true'>
  24. </bm-marker>
  25. </baidu-map>
  26. <el-row class="map_title">
  27. <el-col :span=""
  28. class="map_title_span">
  29. <p>经度:{{locationdata.center ? locationdata.center.lng : center.lng}}</p>
  30. <p>纬度:{{locationdata.center ? locationdata.center.lat : center.lat}}</p>
  31. <p>地址:{{locationdata.addr}}</p>
  32. </el-col>
  33.  
  34. <el-col :span="">
  35. <el-button type="info"
  36. size="small"
  37. @click="getmapdamodata()">提交</el-button>
  38. </el-col>
  39. </el-row>
  40. </el-dialog>
  41.  
  42. </div>
  43. </template>
  44. <script>
  45. export default {
  46. data () {
  47. return {
  48. fullscreenLoading: false,//地图加载动画
  49. // 地图初始化的位置
  50. center: { lng: 113.18088529892, lat: 23.460952009562 },
  51. // 地图内的大小
  52. zoom: ,
  53. // 这个是搜索下的东西
  54. location: '',
  55. // 这个是选择搜索列表中数据的title以及经纬度
  56. locationdata: {
  57. title: '',
  58. center: '',
  59. addr: ''//地址
  60. },
  61. geolocation: "",
  62. BMap: '',
  63. textMap: {
  64. map: '地图'
  65. },
  66. dialogStatus: 'map',
  67. mapCPM: false,
  68. inputaddr: ''
  69. }
  70. },
  71. mounted () {
  72.  
  73. },
  74. methods: {
  75. // 由于百度地图 JS API 只有 JSONP 一种加载方式,因此 BaiduMap 组件及其所有子组件的渲染只能是异步的。因此,请使用在组件的 ready 事件来执行地图 API 加载完毕后才能执行的代码,不要试图在 vue 自身的生命周期中调用 BMap 类,更不要在这些时机修改 model 层。
  76. handler ({ BMap, map }) {
  77. const loading = this.$loading({//加载动画
  78. lock: true,
  79. text: '动图加载中',
  80. spinner: 'el-icon-loading',
  81. background: 'rgba(0, 0, 0, 0.7)'
  82. });
  83.  
  84. window.map = map; //注册为全局
  85. var that = this; // map方法中的this指向不对。所有申明一个。。小细节的东西
  86. // 刚进入页面中的定位,需要向用户授权
  87. var geolocation = new BMap.Geolocation();
  88. console.log(geolocation)
  89. this.geolocation = geolocation;
  90. geolocation.getCurrentPosition(() => {
  91. // console.log('data')
  92. // alert('nimamaipi')
  93. })
  94. geolocation.enableSDKLocation();
  95. geolocation.getCurrentPosition(function (r) {
  96.  
  97. if (this.getStatus() == BMAP_STATUS_SUCCESS) {
  98. // 把得到的经纬度传给map,就实现了第一步我们的定位
  99. that.center = {
  100. lng: r.point.lng,
  101. lat: r.point.lat
  102. }
  103. console.log("wang", r)
  104. // var a = r.address.city
  105. // var b = r.address.district
  106. // var c = r.address.province
  107. // var d = r.address.street
  108. // var e = r.address.street_number
  109.  
  110. // console.log(a);
  111. // console.log(b);
  112. // console.log(c);
  113. // console.log(d);
  114. // console.log(e);
  115.  
  116. // var f = ''
  117. // this.locationdata.addr = ''
  118. // f = a + b + c + d + e
  119. // this.locationdata.addr = f
  120. // console.log(f);
  121.  
  122. // this.locationdata.addr = r.address.city + r.address.district + r.address.province + r.address.street + r.address.street_number
  123. // 当用户拒绝该授权的时候,依然执行
  124. if (r.accuracy == null) {
  125. // alert('accuracy null:'+r.accuracy);
  126. //用户决绝地理位置授权
  127. return;
  128. }
  129. } else {
  130. console.log('failed' + this.getStatus());
  131. }
  132. }, { enableHighAccuracy: true })
  133. this.BMap = BMap
  134. loading.close();
  135. },
  136. mapCpm () {
  137. //打开地图弹窗
  138. this.dialogStatus = 'map'
  139. this.mapCPM = true
  140. },
  141. locationSuccess (point, AddressComponent, marker) {
  142. //定位成功后
  143. console.log(point);
  144. this.locationdata.center = point.point
  145. this.locationdata.addr = point.addressComponent.city + point.addressComponent.district + point.addressComponent.province + point.addressComponent.street + point.addressComponent.streetNumber
  146. this.center = point.point
  147. },
  148. // 选择localtion的值
  149. // getlocalsearch (e) {
  150. // this.locationdata.title = e.address + e.title;
  151. // this.locationdata.center = e.point;
  152. // },
  153. dragend (type, target, pixel, point) { //拖拽结束触发
  154. this.locationdata.center = type.point;
  155. // this.position = type.point
  156. const _this = this
  157. const gc = new this.BMap.Geocoder()
  158. gc.getLocation(type.point, function (rs) {
  159. console.log("aaaaaaaaaaaaaaa", rs)
  160. _this.locationdata.addr = rs.address
  161. })
  162. },
  163. // 确定该信息然后存在session中
  164. getmapdamodata () {
  165. this.inputaddr = this.locationdata.addr
  166. this.mapCPM = false
  167. }
  168.  
  169. }
  170. }
  171. </script>
  172. <style>
  173. .map {
  174. width: %;
  175. height: 500px;
  176. }
  177. .anchorBL {
  178. display: none;
  179. }
  180. .map_title {
  181. display: flex;
  182. justify-content: center;
  183. align-items: center;
  184. padding: .1533rem;
  185. }
  186. </style>

main.js

  1. import BaiduMap from 'vue-baidu-map'
  2.  
  3. Vue.use(BaiduMap, {
  4. // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
  5. ak: '百度ak'
  6. });

更新

  1. <template>
  2. <div>
  3.  
  4. <el-button @click="mapCpm">点击</el-button>
  5. <el-dialog :modal-append-to-body="false"
  6. :title="textMap[dialogStatus]"
  7. :visible.sync="mapCPM">
  8.  
  9. <baidu-map class="map"
  10. :center="center"
  11. :zoom="zoom"
  12. @ready="handler"
  13. :double-click-zoom='false'
  14. :scroll-wheel-zoom='true'
  15. @click="clickBaiDuMap">
  16.  
  17. <bm-view class="map"></bm-view>
  18. <bm-control :offset="{width: '10px', height: '10px'}">
  19. <bm-auto-complete v-model="keyword"
  20. :sugStyle="{zIndex: 0}">
  21. <el-input placeholder="请输入地名关键字"
  22. v-model="keyword"></el-input> <!-- 这里指代一个自定义搜索框组件 -->
  23. <bm-local-search :keyword="keyword"
  24. :auto-viewport="true"
  25. style="width:0px;height:0px;opacity:0"></bm-local-search>
  26.  
  27. </bm-auto-complete>
  28. </bm-control>
  29.  
  30. <bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
  31. :showAddressBar="true"
  32. :autoLocation="true"
  33. @locationSuccess='locationSuccess'></bm-geolocation>
  34. <bm-panorama></bm-panorama>
  35. <bm-marker :position="locationdata.center"
  36. @dragend="dragend"
  37. :raiseOnDrag='true'
  38. :dragging='true'>
  39. </bm-marker>
  40. </baidu-map>
  41. <el-row class="map_title">
  42. <el-col :span=""
  43. class="map_title_span">
  44. <p>经度:{{locationdata.center ? locationdata.center.lng : center.lng}}</p>
  45. <p>纬度:{{locationdata.center ? locationdata.center.lat : center.lat}}</p>
  46. <p>地址:{{locationdata.addr}}</p>
  47. </el-col>
  48.  
  49. <el-col :span="">
  50. <el-button type="info"
  51. size="small"
  52. @click="getmapdamodata()">提交</el-button>
  53. </el-col>
  54. </el-row>
  55. </el-dialog>
  56. </div>
  57. </template>
  58. <script>
  59. export default {
  60. data () {
  61. return {
  62. keyword: '',
  63. city: '',//输入框搜索内容
  64. address_detail: null, //详细地址
  65. fullscreenLoading: false,//地图加载动画
  66. // 地图初始化的位置
  67. center: { lng: 113.18088529892, lat: 23.460952009562 },
  68. // 地图内的大小
  69. zoom: ,
  70. // 这个是搜索下的东西
  71. location: '',
  72. // 这个是选择搜索列表中数据的title以及经纬度
  73. locationdata: {
  74. title: '',
  75. center: '',
  76. addr: ''//地址
  77. },
  78. geolocation: "",
  79. BMap: '',
  80. textMap: {
  81. map: '地图'
  82. },
  83. dialogStatus: 'map',
  84. mapCPM: false,
  85. inputaddr: ''
  86. }
  87. },
  88. mounted () {
  89.  
  90. },
  91. methods: {
  92. // 由于百度地图 JS API 只有 JSONP 一种加载方式,因此 BaiduMap 组件及其所有子组件的渲染只能是异步的。因此,请使用在组件的 ready 事件来执行地图 API 加载完毕后才能执行的代码,不要试图在 vue 自身的生命周期中调用 BMap 类,更不要在这些时机修改 model 层。
  93. handler ({ BMap, map }) {
  94. const loading = this.$loading({//加载动画
  95. lock: true,
  96. text: '动图加载中',
  97. spinner: 'el-icon-loading',
  98. background: 'rgba(0, 0, 0, 0.7)'
  99. });
  100.  
  101. window.map = map; //注册为全局
  102. var that = this; // map方法中的this指向不对。所有申明一个。。小细节的东西
  103. // 刚进入页面中的定位,需要向用户授权
  104. var geolocation = new BMap.Geolocation();
  105. console.log(geolocation)
  106. this.geolocation = geolocation;
  107. geolocation.getCurrentPosition(() => {
  108. // console.log('data')
  109. // alert('nimamaipi')
  110. })
  111. geolocation.enableSDKLocation();
  112. geolocation.getCurrentPosition(function (r) {
  113.  
  114. if (this.getStatus() == BMAP_STATUS_SUCCESS) {
  115. // 把得到的经纬度传给map,就实现了第一步我们的定位
  116. that.center = {
  117. lng: r.point.lng,
  118. lat: r.point.lat
  119. }
  120. console.log("wang", r)
  121.  
  122. var a = r.address.city
  123. var b = r.address.district
  124. var c = r.address.province
  125. var d = r.address.street
  126. var e = r.address.street_number
  127.  
  128. console.log(a);
  129. console.log(b);
  130. console.log(c);
  131. console.log(d);
  132. console.log(e);
  133.  
  134. var f = ''
  135. //this.locationdata.addr = ''
  136. f = a + b + c + d + e
  137. window.title = f;//注册为全局
  138. // this.locationdata.addr = f
  139. console.log(f);
  140. // this.locationdata.addr = r.address.city + r.address.district + r.address.province + r.address.street + r.address.street_number
  141. // 当用户拒绝该授权的时候,依然执行
  142. if (r.accuracy == null) {
  143. // alert('accuracy null:'+r.accuracy);
  144. //用户决绝地理位置授权
  145. return;
  146. }
  147. } else {
  148. console.log('failed' + this.getStatus());
  149. }
  150. }, { enableHighAccuracy: true })
  151. this.BMap = BMap
  152. loading.close();
  153. },
  154. mapCpm () {
  155. //打开地图弹窗
  156. setTimeout(() => {
  157. if (window.title != '' && window.title != null) {
  158. this.locationdata.addr = window.title
  159. window.title = ''
  160. }
  161. }, )
  162.  
  163. console.log(window.title);
  164.  
  165. this.dialogStatus = 'map'
  166. this.mapCPM = true
  167. },
  168. locationSuccess (point, AddressComponent, marker) {
  169. //定位成功后
  170. console.log(point);
  171. this.locationdata.center = point.point
  172. this.locationdata.addr = point.addressComponent.city + point.addressComponent.district + point.addressComponent.province + point.addressComponent.street + point.addressComponent.streetNumber
  173. this.center = point.point
  174. },
  175. // 选择localtion的值
  176. // getlocalsearch (e) {
  177. // this.locationdata.title = e.address + e.title;
  178. // this.locationdata.center = e.point;
  179. // },
  180. dragend (type, target, pixel, point) { //拖拽结束触发
  181. this.locationdata.center = type.point;
  182. // this.position = type.point
  183. const _this = this
  184. const gc = new this.BMap.Geocoder()
  185. gc.getLocation(type.point, function (rs) {
  186. console.log("aaaaaaaaaaaaaaa", rs)
  187. _this.locationdata.addr = rs.address
  188. })
  189. },
  190. // 确定该信息然后存在session中
  191. getmapdamodata () {
  192. this.inputaddr = this.locationdata.addr
  193. this.mapCPM = false
  194. },
  195. clickBaiDuMap (type, target, point, pixel, overlay) {
  196. console.log(type);
  197. console.log(target); console.log(point); console.log(pixel); console.log(overlay);
  198. this.locationdata.center = type.point;
  199. // this.center = type.point
  200. // this.position = type.point
  201. const _this = this
  202. const gc = new this.BMap.Geocoder()
  203. gc.getLocation(type.point, function (rs) {
  204. console.log("aaaaaaaaaaaaaaa", rs)
  205. _this.locationdata.addr = rs.address
  206. })
  207. },
  208.  
  209. }
  210. }
  211. </script>
  212. <style scoped>
  213. .map {
  214. width: %;
  215. height: 500px;
  216. }
  217. .anchorBL {
  218. display: none;
  219. }
  220. .map_title {
  221. display: flex;
  222. justify-content: center;
  223. align-items: center;
  224. padding: .1533rem;
  225. }
  226. </style>

vue中引入百度地图的更多相关文章

  1. VUE 中引入百度地图(vue-Baidu-Map)

    1.安装 $ npm install vue-baidu-map --save 2.全局注册,在main.js中引入以下代码 import BaiduMap from 'vue-baidu-map' ...

  2. Vue中使用百度地图——设置地图标注

    知识点:创建Map实例,为指定的位置设置标注 参考博客:https://www.cnblogs.com/liuswi/p/3994757.html 1.效果图:初始化地图,设置指定经纬度为地图中心点坐 ...

  3. vue中实现百度地图

    1.项目根目录下下载百度地图插件 npm install vue-baidu-map –save 2.在首页index.html中引入百度地图: <script type="text/ ...

  4. Vue --》 如何在vue中调用百度地图

    1.项目根目录下下载百度地图插件 npm install vue-baidu-map –save 2.在首页index.html中引入百度地图: <script type="text/ ...

  5. Vue中使用百度地图——根据输入框输入的内容,获取详细地址

    知识点:在Vue.js项目中调用百度地图API,实现input框,输入地址,在百度地图上定位到准确地址,获得到经纬度 参考博客:  百度地图的引用,初步了解参考博客:http://blog.csdn. ...

  6. vue中使用百度地图vue-baidu-map

    安装 npm install vue-baidu-map --save 全局注册 全局注册将一次性引入百度地图组件库的所有组件.需在入口文件main.js中引入vue-baidu-map import ...

  7. vue中引入百度统计

    vue作为单页面的,引入百度统计,需要注意不少. 一.基本的流量统计 在index.html 入口文件中引入百度统计生成的一连串代码: var _hmt = _hmt || []; (function ...

  8. react项目中引入百度地图打包报错问题

    一.我正常引入百度地图,调试时候是好使的,但是打包时候就报错 引入方法如下: 报错如图 正常调试是好使的,但是打包报这个错,解析不了这个BMap,那么怎么办呢? 然后我就转用了window办法,虽然因 ...

  9. vue 中结合百度地图获取当前城市

    首先需要去百度地图开发者平台申请 ak http://lbsyun.baidu.com/index.php?title=%E9%A6%96%E9%A1%B5 在index.html 中引入script ...

随机推荐

  1. 【数论】[涨姿势:同余]P2312解方程

    题目描述 已知多项式方程:\(a_0 + a_1x + a_2x^2+...+a_nx^n = 0\) 求这个方程在[1,m]内的整数解 \(1\leq n\leq100,|a_i|\leq 10^{ ...

  2. SSH登录慢解方案 - 关闭UseDNS加速

    每次登录SSH时总是要停顿等待一会儿才能连接上,,这是因为OpenSSH服务器有一个DNS查找选项UseDNS默认情况下是打开的. UseDNS 选项打开状态下,当通过终端登录SSH服务器时,服务器端 ...

  3. Spring中静态方法中使用@Resource注解的变量

    开发中,有些时候可能会工具类的静态方法,而这个静态方法中又使用到了@Resource注解后的变量.如果要直接使用 Utils.staticMethod(),项目会报异常:如果不直接使用,还要先 new ...

  4. javaweb利用filter拦截未授权请求

    项目上有个小需求,要限制访问者的IP,屏蔽未授权的请求.该场景使用过滤器来做再合适不过了. SecurityFilter.java: public class SecurityFilter imple ...

  5. MYSQL避免重复插入记录的三种方法

      方案一:使用ignore关键字 如果是用主键primary或者唯一索引unique区分了记录的唯一性,避免重复插入记录可以使用: insert ignore into table_name(ema ...

  6. 模拟26A 题解

    A. marshland 考试时想到了网络流,然而不会建图,就死了. 正解是最大费用可行流. 比较容易想到的是将每个点拆为两个点, s连没有危险值的入点, 没有危险值的入点连有危险值的入点,入点出点之 ...

  7. spring的重试策略、发生异常会自动重新调用

    测试类以及测试代码.复制即可 package com.cms.util; import javax.swing.plaf.synth.SynthSpinnerUI; import org.spring ...

  8. .NETCore websocket

    .NETCore websocket 实现简易.高性能.集群即时通讯组件,支持点对点通讯.群聊通讯.上线下线事件消息等众多实用性功能. https://github.com/2881099/im

  9. 每天一个命令-cp 命令

    cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录.它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下.cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文 ...

  10. vue的跳转方式(打开新页面)及传参

    1. router-link跳转 // 直接写上跳转的地址 <router-link to="/detail/one"> <span class="sp ...