vue中使用baidu-map:根据详细地址、经纬度进行地图定位

1.全局安装与引用
(1)安装

npm install vue-baidu-map --save

(2)全局引用

import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
  ak: '4258*******************97ddfd'
})

2.使用
(1)HTML

<div style="width: 50%">
            <el-form-item label="详细地址" :required="true" prop="address">
              <el-input
                v-model="formName.address"
                :readonly="readonly"
                class="from_detailed"
                placeholder="请输入详细地址"
              />
            </el-form-item>
          </div>
<div class="nwe_shop_store" style="width: 50%">
            <div class="from_longitudes">
              <el-form-item label="经度" :required="true" prop="longitude">
                <el-input
                  v-model="formName.longitude"
                  placeholder="输入或选择商城或点击地图获取"
                  :readonly="readonly"
                  class="from_longitude"
                  @change="scanMap"
                />
              </el-form-item>
              <el-form-item label="纬度" :required="true" prop="longitude">
                <el-input
                  v-model="formName.latitude"
                  placeholder="输入或选择商城或点击地图获取"
                  :readonly="readonly"
                  class="from_longitude"
                  @change="scanMap"
                />
              </el-form-item>
            </div>
          </div>
          <el-form-item label="定位" :required="true">
            <!-- <el-input v-model="formName.location" class="from_location" /> -->
            <div v-loading="loadingMap" class="dw">
              <baidu-map
                :center="center"
                :zoom="zoom"
                :scroll-wheel-zoom="true"
                class="dw"
                @click="getLocationPoint"
                @ready="handler"
              >
                <bm-view style="width: 100%; height: 100%; flex: 1" />
                <bm-local-search
                  :keyword="formName.address"
                  :auto-viewport="true"
                  style="display: none"
                />
              </baidu-map>
            </div>
          </el-form-item>

(2)Script中DATA参数定义

 center: {
   lng: 113.027698,
   lat: 28.144912,
  }, // 定位信息
 zoom: 13, // 定位信息
 formName: {
  location: '',
  longitude: '',
  latitude: '',
  address: ''
 }

(3)Script中的Methods方法

getLocationPoint(e) {
      // 点击触发搜索
      // eslint-disable-next-line no-undef
      const geoCoder = new BMap.Geocoder();
      // 获取位置对应的坐标
      geoCoder.getPoint(this.addressKeyword, (point) => {
        this.selectedLng = point.lng;
        this.selectedLat = point.lat;
        // console.log(point)
      });
      // 利用坐标获取地址的详细信息
      geoCoder.getLocation(e.point, (res) => {
        console.log(res);
        // this.formName.address = res.address // 赋值地址
        this.formName.longitude = res.point.lng; // 赋值经度
        this.formName.latitude = res.point.lat; // 纬度
      });
      setTimeout(() => {
        this.scanMap();
      }, 500);
    },
    handler({ BMap, map }) {
      console.log("map函数");
      this.BMap = BMap;
      this.map = map;
      this.scanMap();
    },
    scanMap() {
      this.map.clearOverlays();
      if (this.formName.longitude === "" || this.formName.latitude === "") {
        // 默认地址
        this.center = {
          lng: 113.027698,
          lat: 28.144912,
        };
        var point = new this.BMap.Point(113.027698, 28.144912); // 根据经纬度创建点坐标
        console.log("经纬度空的");
      } else {
        this.center = {
          lng: this.formName.longitude,
          lat: this.formName.latitude,
        };
        // eslint-disable-next-line no-redeclare
        var point = new this.BMap.Point(
          this.formName.longitude,
          this.formName.latitude
        ); // 根据经纬度创建点坐标
      }
      this.map.centerAndZoom(point, 17);
      var marker = new this.BMap.Marker(point); // 创建标注
      this.map.addOverlay(marker); // 将标注添加到地图中
      this.zoom = 17;
    },

因项目需求,通过多方查找资料与交流所得。如有侵权,请联系删除!

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"vue-baidu-map-3x"是一个基于Vue.js的百度地图组件库,它提供了一系列的组件和指令,方便开发者在Vue.js项目使用百度地图功能。 要通过经纬度进行跳转,可以使用该组件库的`<baidu-map>`组件和`<baidu-marker>`组件来实现。首先,在Vue.js项目安装并引入"vue-baidu-map-3x"组件库,然后在需要使用地图的页面使用`<baidu-map>`组件来显示地图,并设置`center`属性为目标经纬度。 接下来,在目标经纬度上添加一个标记点,可以使用`<baidu-marker>`组件,并设置`position`属性为目标经纬度。同时,可以设置该标记点的其他属性,如图标、标题等。 最后,通过点击标记点或其他交互方式触发跳转事件,在事件处理函数使用百度地图的API进行跳转操作。可以使用`map.panTo()`方法将地图心移动到目标经纬度,或者使用`map.setZoom()`方法设置地图缩放级别。 具体代码示例如下: ```html <template> <div> <baidu-map :ak="yourBaiduMapAK" :center="targetLatLng" :zoom="15"> <baidu-marker :position="targetLatLng" @click="jumpToTarget"></baidu-marker> </baidu-map> </div> </template> <script> import { BaiduMap, BaiduMarker } from 'vue-baidu-map-3x'; export default { components: { BaiduMap, BaiduMarker, }, data() { return { yourBaiduMapAK: 'yourBaiduMapAK', targetLatLng: { lng: 123.456, // 目标经度 lat: 12.345, // 目标纬度 }, }; }, methods: { jumpToTarget() { // 获取地图实例 const map = this.$refs.baiduMap.getMapInstance(); // 将地图心移动到目标经纬度 map.panTo(new BMap.Point(this.targetLatLng.lng, this.targetLatLng.lat)); // 设置地图缩放级别 map.setZoom(15); }, }, }; </script> ``` 请注意,上述代码的`yourBaiduMapAK`需要替换为你自己的百度地图开发者AK(Access Key),用于在使用百度地图API时进行身份验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值