在 Vue 中使用百度地图实现打点获取经纬度等

1. 安装

npm i vue-baidu-map -S

点击查看官方文档


2. 注册组件

main.js

// 全局注册百度地图组件

import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, { ak: 'your_ak' }) // 自行申请

// 按需引入
根据官方文档来

3. 封装组件

src/component/Map.vue

实现一个简单 demo, css 自己写

<template>
  <div class="map-wrapper">
    <input type="text" v-model="mapSearchConfig.keyword" />
    <baidu-map v-bind="mapConfig" v-on="bindMapEvent">
      <bm-local-search v-bind="mapSearchConfig"></bm-local-search>
    </baidu-map>
    <div class="map-info">
      <div class="map-address">{{ markerPo.address }}</div>
      <div class="lng-lat" v-if="markerPo.lng">
        <div>经度:{{ markerPo.lng.toFixed(5) }}</div>
        <div>纬度:{{ markerPo.lat.toFixed(5) }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TheMap',
  data() {
    return {
      map: undefined, // map 实例
      mapConfig: {
        center: { lng: 119.98, lat: 30.548 }, // 初始位置
        zoom: 14, // 缩放等级
        scrollWheelZoom: true, // 允许滚轮缩放地图
        style: 'height:200px; width:500px;',
      },
      /* v-on="{ }" 使用对象语法时不支持任何修饰器 */
      bindMapEvent: {
        rightclick: this.getClickInfo,
        ready: this.onBMapReady,
      },
      mapSearchConfig: {
        keyword: '',
        location: '德清', // 搜索范围
        panel: false, // 是否展开自带搜索结果面板
        autoViewport: true, // 搜索后自动跳转视角
      },
      //点击过后获取的地址信息
      markerPo: {
        address: '请于地图上右击鼠标选择具体位置!',
      },
      marker: null, // 标记点
    };
  },
  props: {
    value: { type: String, default: '' }, // 获取后端数据中已存在的点位信息
  },
  watch: {
    value() {
      this.initData();
    },
  },
  methods: {
    initData() {
      if (!this.map || !this.value) {
        return;
      }
      const arr = this.value.split(',');
      if (arr.length === 3) {
        const [address, lng, lat] = arr;
        this.mapConfig.center = { lng, lat };
        this.markerPo = { address, lng, lat };
        this.addMarker(this.markerPo);
      }
    },
    onBMapReady({ map }) {
      this.map = map;
      this.initData();
    },
    getClickInfo(e) {
      // 反编码通过点击经纬度获取地名地址
      let geoCoder = new BMap.Geocoder();
      geoCoder.getLocation(e.point, res => {
        const { address, business, point } = res;
        this.markerPo = {
          address: `${address} ${business ? `(${business})` : ''}`,
          ...point,
        };
        this.addMarker(this.markerPo);

        /* 下边的代码只取结果第一个会导致一片地方可能始终只有一个经纬度和点位 */

        // 获取地名
        // let current_point = {};
        // current_point = res.surroundingPois.length
        //   ? res.surroundingPois[0]
        //   : res;
        // const { address, title } = current_point;
        // const addressName = `${title ? title + ' (' : ''}${address}${
        //   title ? ')' : ''
        // }`;
        // this.markerPo = {
        //   ...current_point.point,
        //   address: addressName,
        // };
        // this.addMarker(this.markerPo);
        // this.$emit(
        //   'input',
        //   `${address},${current_point.point.lng},${current_point.point.lat}`,
        // );
      });
    },
    addMarker(data) {
      // 移除地图上所有点
      this.marker && this.map.removeOverlay(this.marker);

      // const point = new BMap.Point(data.lng, data.lat);
      // this.marker = new BMap.Marker(point); // 创建标注

      this.marker = new BMap.Marker(data); // 创建标注
      this.map.addOverlay(this.marker); // 将标注添加到地图中
    },
  },
};
</script>
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue使用百度地图API自动获取地址和经纬度,你需要完成以下步骤: 1. 在百度地图开放平台上注册账号并创建应用,获取API密钥。 2. 在Vue项目引入百度地图API的JS文件,在index.html添加以下代码: ``` <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=your_api_key"></script> ``` 将 `your_api_key` 替换为你在百度地图开放平台上创建应用后获取的API密钥。 3. 在Vue组件使用以下代码,创建地图实例: ``` mounted() { var map = new BMap.Map("map-container"); this.map = map; }, ``` 其,`map-container` 是放置地图的DOM元素的ID。 4. 在Vue组件使用以下代码,获取用户当前位置的经纬度: ``` mounted() { var map = new BMap.Map("map-container"); this.map = map; var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition((position) => { if (geolocation.getStatus() === BMAP_STATUS_SUCCESS) { this.latitude = position.point.lat; this.longitude = position.point.lng; } else { alert('定位失败'); } }); }, ``` 其,`geolocation.getCurrentPosition` 方法用于获取用户当前位置的经纬度。如果获取成功,则将经纬度赋值给 `latitude` 和 `longitude` 变量。如果获取失败,则弹出提示框。 5. 在Vue组件使用以下代码,将经纬度转换成具体的地址: ``` mounted() { var map = new BMap.Map("map-container"); this.map = map; var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition((position) => { if (geolocation.getStatus() === BMAP_STATUS_SUCCESS) { this.latitude = position.point.lat; this.longitude = position.point.lng; var point = new BMap.Point(this.longitude, this.latitude); var geoc = new BMap.Geocoder(); geoc.getLocation(point, (rs) => { this.address = rs.address; }); } else { alert('定位失败'); } }); }, ``` 其,`BMap.Point` 方法用于创建一个地理位置点的实例,需要传入经度和纬度。`BMap.Geocoder` 方法用于将经纬度转换成具体的地址。`geoc.getLocation` 方法用于获取地址信息,并将地址信息赋值给 `address` 变量。 这样,你就可以在Vue使用百度地图API自动获取地址和经纬度了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值