vue3+vue-baidu-map-3x 实现地图定位

文档地址:一个是2一个是3

https://dafrok.github.io/vue-baidu-map/#/zh/index

vue-baidu-map-3x

1.首先要到百度地图开放平台上建一个账号,如果有百度账号可以直接登录百度地图-百万开发者首选的地图服务商,提供专属的行业解决方案

2.点击控制台,完善信息

3.信息完善之后,打开的是这个页面(我这里完善的信息选择的是个人)

4.然后点击应用管理下的我的应用,此时打开的页面如果已有应用可以直接用那个ak值,如果没有请点击创建应用,根据自己的需要进行选择生成,第三张图片上的白名单,如果本地跑的话可以直接输入*,但要注意上线之后改掉,创建好后就有一个ak啦,一会在代码里把这个ak粘进去就可以啦

5,程序:我这里用了两个功能,一个是定位,一个是搜索

先安装

复制代码
pnpm install vue-baidu-map-3x --save

html

复制代码
<!-- 百度地图 -->
    <baidu-map
      class="map"
      ak="L3on1qvFT4RG1weFYRIxGbG8WOXpqzAi"
      v="3.0"
      type="API"
      :zoom="15"
      :center="centerVal"
    >
      <!-- 定位 -->
      <bm-geolocation
        anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
        :show-address-bar="true"
        :auto-location="true"
        @locationSuccess="successFn"
        @locationError="errorFn"
      />
      <!-- 自定义组件 -->
      <bm-control>
        <a-input-search
          v-model:value="addressSear"
          placeholder="请输入地点"
          enter-button="搜索"
          size="large"
          @search="onSearch"
        />
      </bm-control>
    </baidu-map>

script

复制代码
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BaiduMap, BmGeolocation, BmControl } from 'vue-baidu-map-3x';
  export default defineComponent({
    name: 'a',
    components: { BaiduMap, BmGeolocation, BmControl },
    emits: ['success'],
    setup(props, { emit }) {
      //搜索框内容
      const addressSear = ref('');
      // 百度地图默认定位
      const centerVal = ref('北京');
      // 定位存储的位置
      const positonStr = ref('');

      // 搜索框-赋值,让地图跟着变化
      const onSearch = () => {
        centerVal.value = addressSear.value;
      };
      // 定位失败
      const errorFn = () => {
        alert('定位失败');
      };
      // 定位成功
      const successFn = (e: any) => {
        // 定位赋值
        positonStr.value =
          e.addressComponent.province +
          e.addressComponent.city +
          e.addressComponent.district +
          e.addressComponent.street +
          e.addressComponent.streetNumber;
      };
      return { errorFn, successFn, addressSear, centerVal, onSearch };
    },
  });

完整程序

复制代码
<template>
  <........
  >
    <!-- 百度地图 -->
    <baidu-map
      class="map"
      ak="L3on1qvFT4RG1weFYRIxGbG8WOXpqzAi"
      v="3.0"
      type="API"
      :zoom="15"
      :center="centerVal"
    >
      <!-- 定位 -->
      <bm-geolocation
        anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
        :show-address-bar="true"
        :auto-location="true"
        @locationSuccess="successFn"
        @locationError="errorFn"
      />
      <!-- 自定义组件 -->
      <bm-control>
        <a-input-search
          v-model:value="addressSear"
          placeholder="请输入地点"
          enter-button="搜索"
          size="large"
          @search="onSearch"
        />
      </bm-control>
    </baidu-map>
  </.............>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BaiduMap, BmGeolocation, BmControl } from 'vue-baidu-map-3x';
  export default defineComponent({
    name: '.........',
    components: { BaiduMap, BmGeolocation, BmControl },
    emits: ['success'],
    setup(props, { emit }) {
      .................
      //搜索绑定变量
      const addressSear = ref('');
      //定位, 可使用如"广州市海珠区"的地区字符串,也可以使用对象如 {lng: 116.404, lat: 39.915} 表示经纬度
      const centerVal = ref('北京');
      // 定位按钮的当前位置
      const positonStr = ref('');
      
      // 弹窗保存
      async function handleSubmit() {
        if (!positonStr.value) {
          positonStr.value = centerVal.value;
        }
        emit('success', positonStr.value);
        ........
      }
      // 搜索框
      const onSearch = () => {
        centerVal.value = addressSear.value;
      };
      // 定位失败
      const errorFn = () => {
        alert('定位失败');
      };
      // 定位成功
      const successFn = (e: any) => {
        // 定位赋值
        positonStr.value =
          e.addressComponent.province +
          e.addressComponent.city +
          e.addressComponent.district +
          e.addressComponent.street +
          e.addressComponent.streetNumber;
      };
      return {  handleSubmit, errorFn, successFn, addressSear, centerVal, onSearch };
    },
  });
</script>
<style scoped>
  .map {
    width: 100%;
    height: 300px;
  }
</style>
相关推荐
Bug从此不上门6 小时前
【无标题】
前端·javascript·uni-app·vue
狼性书生1 天前
uniapp实现的简约美观的星级评分组件
前端·uni-app·vue·组件
宇宙的最后一粒尘埃1 天前
Typeerror: cannot read properties of undefined (reading ‘XXX‘)
vue
清幽竹客2 天前
vue-18(使用 Vuex 插件实现高级功能)
前端·vue.js·前端框架·vue
牧码岛2 天前
Web前端之隐藏元素方式的区别、Vue循环标签的时候在同一标签上隐藏元素的解决办法、hidden、display、visibility
前端·css·vue·html·web·web前端
MINO吖2 天前
基于 qiankun + vite + vue3 构建微前端应用实践
vue·vite·微前端·qiankun·single-spa
Luffe船长2 天前
elementUI点击浏览table所选行数据查看文档
javascript·elementui·vue
IT瘾君3 天前
JavaWeb:前端工程化-ElementPlus
前端·elementui·node.js·vue
sunbyte4 天前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | Dad Jokes(冷笑话卡片)
前端·javascript·css·vue.js·vue
幽络源小助理4 天前
SpringBoot+Vue+微信小程序校园自助打印系统
java·spring boot·微信小程序·小程序·vue