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>
相关推荐
蜚鸣3 天前
Vue的快速入门
vue
吃饭最爱5 天前
⽹络请求Axios的概念和作用
vue
小咪一会5 天前
JVM 基础
jvm·1024程序员节
魂尾ac5 天前
Django + Vue3 前后端分离技术实现自动化测试平台从零到有系列 <第一章> 之 注册登录实现
后端·python·django·vue
是罐装可乐5 天前
深入理解 Vue3 Router:三种路由模式的工作原理与实战应用
架构·vue·路由·history·hash·ssr·router
老华带你飞5 天前
租房平台|租房管理平台小程序系统|基于java的租房系统 设计与实现(源码+数据库+文档)
java·数据库·小程序·vue·论文·毕设·租房系统管理平台
zhz52146 天前
Spring Boot + Redis 缓存性能优化实战:从5秒到毫秒级的性能提升
java·spring boot·redis·缓存·vue
小胖墩有点瘦7 天前
【基于协同过滤的校园二手交易平台】
java·vue·毕业设计·springboot·计算机毕业设计·协同过滤·校园二手交易平台
小圣贤君7 天前
小说创作中的时间轴体验设计:事序图交互与用户体验优化
electron·vue·甘特图·时序图·写作软件
知识分享小能手7 天前
React学习教程,从入门到精通,React 构造函数(Constructor)完整语法知识点与案例详解(16)
前端·javascript·学习·react.js·架构·前端框架·vue