首页 > 其他分享 >vue接入百度地图

vue接入百度地图

时间:2022-08-25 18:23:00浏览次数:95  
标签:vue center point 接入 result lat lng 百度

1.需要在百度地图API获取密钥(AK)

2.安装 npm install vue-baidu-map --save

3.全局引入 

import Vue from 'vue'
import BaiduMap from 'vue-baidu-map'
 
Vue.use(BaiduMap, {
ak: 'YOUR_APP_KEY' // ak 是在百度地图开发者平台申请的密钥
 })

4.组件中使用

<template>
  <div class="BaiDuMap">
    <baidu-map
      :center="center"
      :zoom="zoom"
      :scroll-wheel-zoom="true"
      style="width: 100%; height: 100%"
      @ready="handler"
      @click="getClickInfo"
      @moving="syncCenterAndZoom"
      @moveend="syncCenterAndZoom"
      @zoomend="syncCenterAndZoom"
    >
      <!-- 必须给容器指高度,不然地图将显示在一个高度为0的容器中,看不到 -->
      <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
      <bm-geolocation
        anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
        :showAddressBar="true"
        :autoLocation="true"
      ></bm-geolocation>
      <bm-city-list anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list>
    </baidu-map>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 地址信息
      address: null,
      center: { lng: 0, lat: 0 },
      //地图展示级别
      zoom: 11,
    };
  },
  methods: {
    handler({ BMap, map }) {
      this.center.lng = 116.404;
      this.center.lat = 39.915;
      this.zoom = this.zoom;
    },
getClickInfo(e) { const myGeo = new BMap.Geocoder(); //创建地理编码实例 // 根据坐标逆解析地址 myGeo.getLocation(new BMap.Point(e.point.lng, e.point.lat), (result) => { console.log(result, "result-->>>>"); if (result) { this.address = result.address; } }); this.center.lng = e.point.lng; this.center.lat = e.point.lat; },
syncCenterAndZoom(e) { console.log(e.target, 'e.target-->>>>') const { lng, lat } = e.target.getCenter(); this.zoom = e.target.getZoom(); }, }, }; </script> <style scoped> .BaiDuMap { width: 100%; height: 100%; } </style>

 

效果展示:

 

标签:vue,center,point,接入,result,lat,lng,百度
From: https://www.cnblogs.com/CaraganaMicrophylla/p/16625292.html

相关文章

  • vue3 学习笔记
    watchletsum=ref('0');letperson=reactive({sex:‘女’,age:18,}) watch(sum,(oldVal,newVal)=>{console.log(oldVal,newVal);})/**监视reactive所定义的一......
  • 【Vue基础】provide和inject 依赖与注入
    Vue组件通信provide和inject,注入使用场景,祖先组件向下层所有组件注入,无论层级多深,子组件均能接收来自祖先组件。某个模块由根组件内统一管理子组件内的状态。类型prov......
  • vue 3.0 国密sm2算法加密解密文件流
     针对文件流加密需要考虑性能问题,所以选择部分文件字节加密,破坏文件内容,达到用户不能随意下载打开文件 ts文件:import{sm2}from'sm-crypto';import{Buffer}......
  • antdesign vue中写了样式不加载的解决方法
    在vue组件里,lang设置为less,在style设置为scoped的时候,在写样式有时候对子组件不生效。如果想让某些样式对子组件生效,可以使用/deep/深度选择器。代码:/deep/.ant-men......
  • vue 国际化i18n在弹出框中$t()报错:TypeError: Cannot read properties of undefined (
    废话不用多说,直接上图:   解决思路如下:在main.js文件中把vue挂载到window。window.vm=newVue({el:'#app',i18n:i18n,...})在弹出的窗口中获取window.vmm......
  • vue socket socketio typescript
    main.ts注册和导出,xx.vue导入使用xx.vue在传递给xxx.vue使用注意类型的定义......
  • vue表格之:summary-method="getSummaries"与show-summary(列求和)
    //表格列求和<el-table:summary-method="getSummaries"show-summary></el-table>getSummaries(param){const{columns,data}=paramconstsums=[]......
  • vue接口获取路由菜单
    menuFormat.js(格式化路由表)exportconstinitMenu=(data)=>{letlist=[];data.forEach(router=>{let{path,comp......
  • Vue项目使用高德地图
    流程:注册账号获取KEY安装使用注册链接:https://lbs.amap.com/选择Web端(JSAPI),同时需要域名限制请按提示进行输入:创建完成后即可看到KEY。安装高德地图JSAPI......
  • vue2的nextTick使用
    1、关于nextTick。vue中的Dom更新是异步的,是异步的意味着当被处理数据是动态变化时,此时对应的Dom未能及时更新(同步更新)就会导致数据已经更新(model层已经更新)而视力层未更......