Vue2-百度地图定位

Vue-百度地图插件

Vue Baidu Map 官网

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

百度地图 javascript API(插件的文档有些并未解释完全,需要结合官方API文档阅读)
密钥申请(必须要申请开发者密钥才可以使用百度地图)

准备工作

安装vue-baidu-map

$ npm install vue-baidu-map --save

安装vue-jsonp(根据地址转换成经纬度需要用到)

$ npm install vue-jsonp

申请秘钥

密钥申请(必须要申请开发者密钥才可以使用百度地图)

准备工作已完成,直接看代码:

// 全局引入百度地图插件
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
 // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
 ak: '你的AK秘钥'
})
// 全局引入vue-jsonp
import {VueJsonp} from 'vue-jsonp'
Vue.use(VueJsonp)
<template>
  <div>
    <baidu-map
      class="box_map"
      :center="locations"
      :zoom="zoom"
      :scroll-wheel-zoom="true"
      @ready="handler"
    >
      <!-- :scroll-wheel-zoom="true" 该属性为鼠标滚轮缩放 -->
      <div class="inputVal">
        <input v-model="addressVal" type="text" />
        <button @click="btn">OK</button>
      </div>
      <!-- 比例尺 -->
      <bm-scale anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-scale>
      <!-- 定位控件 -->
      <bm-geolocation
        anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
        :showAddressBar="true"
        :autoLocation="true"
        @locationSuccess="locationSuccess"
      ></bm-geolocation>
      <!-- 标点 -->
      <bm-marker
        :position="{ lng: locations.lng, lat: locations.lat }"
        :dragging="true"
        animation="BMAP_ANIMATION_BOUNCE"
      >
      </bm-marker>
    </baidu-map>
  </div>
</template>

<script>
// let timeId;
export default {
  data() {
    return {
      center: "",
      locations: {
        // 红色标点的经纬度需要设一个初始值,不然地图无法渲染处理
        lng: 0,
        lat: 0,
      },
      zoom: 3,
      addressVal: "",
    };
  },
  created() {
    this.geoTest();
  },
  methods: {
    handler({ BMap, map }) {
      console.log(55, BMap, map);
      this.center = "";
      this.zoom = 15;
      // 获取IP地址的经纬度,详情查看官方文档:
      // https://mapopen-pub-jsapi.bj.bcebos.com/jsapi/reference/jsapi_webgl_1_0.html#a8b42
      const geolocation = new BMap.Geolocation();
      geolocation.getCurrentPosition((res) => {
        // IP地址赋值给locations对象
        this.locations.lng = res.point.lng;
        this.locations.lat = res.point.lat;
      });
    },
    locationSuccess() {
      console.log(`定位成功`);
    },
    async geoTest() {
      // 调用百度地图API,根据地址获取经纬度
      await this.$jsonp("http://api.map.baidu.com/geocoding/v3/", {
        address: this.addressVal, // input框输入的地址
        output: "json",
        ak: "你的AK秘钥", // 你的AK秘钥
      })
        .then((json) => {
          console.log(`json success:`, json);
          this.locations = json.result.location;
        })
        .catch((err) => {
          // clearTimeout(timeId);
          // if (err) {
          //   timeId = setTimeout(() => {
          //     this.geoTest();
          //   }, 2000);
          // }
          console.log(`json err:`, err);
        });
    },
    // 修改地址
    btn() {
      this.geoTest();
    },
  },
};
</script>

<style scoped>
.box_map {
  margin: 30px auto;
  width: 1000px;
  height: 600px;
  background-color: #f0f;
  position: relative;
}
.inputVal {
  position: absolute;
  left: 0;
  top: 0;
  width: 50%;
}

input {
  width: 60%;
}
button {
  margin-top: 10px;
  margin-left: 20px;
  width: 40px;
  height: 30px;
}
</style>

效果图:

在这里插入图片描述

要在Vue2中使用百度地图进行定位,可以按照以下步骤进行操作。 首先,你需要在项目中安装`vue-baidu-map`插件。你可以使用以下命令进行安装: ``` npm install vue-baidu-map --save ``` 接下来,在你的`main.js`文件中引入`vue-baidu-map`并注册插件,同时配置你在百度地图开发者平台申请的密钥。你可以使用以下代码: ``` import Vue from 'vue' import BaiduMap from 'vue-baidu-map' Vue.use(BaiduMap, { ak: 'YOUR_APP_KEY' // 这里替换成你在百度地图开发者平台申请的密钥 }) ``` 然后,在你需要使用百度地图的组件中,可以通过`<baidu-map>`标签来引入地图,并设置相应的属性来进行定位。例如,你可以在组件的模板中添加以下代码: ``` <template> <div> <baidu-map :center="center" :zoom="zoom" style="width: 100%; height: 400px;"></baidu-map> </div> </template> <script> export default { data() { return { center: { // 设置地图中心点的经纬度 lng: 116.404, lat: 39.915 }, zoom: 14 // 设置地图缩放级别 } } } </script> ``` 以上代码中的`center`属性可以设置地图的中心点的经纬度,`zoom`属性可以设置地图的缩放级别。你可以根据需要进行调整。 最后,你可以根据你的实际需求,进一步自定义百度地图的样式和功能。你可以参考[官方文档](https://dafrok.github.io/vue-baidu-map/#/zh/control/city-list)了解更多关于如何在Vue2中使用百度地图定位功能的详细信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue3使用百度地图获取位置信息](https://blog.csdn.net/sg_knight/article/details/129055556)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vue2实现百度地图定位](https://blog.csdn.net/qq_44278289/article/details/127669096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值