在vue项目中使用百度地图,点击或搜索打点组件封装

一、在index.html文件中引入百度地图

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vue 2 BMap Demo</title>
  <script src="https://api.map.baidu.com/api?v=3.0&amp;ak=你的_KEY"></script>
</head>
<body>
  <div id="app"></div>
  <script src="/dist/build.js"></script>
</body>
</html>

二、创建map.vue地图组件

<template>
  <div class="bmap-search-marker">
    <input type="text" v-model="keyword" placeholder="请输入关键字">
    <button @click="search">搜索</button>
    <div ref="mapContainer" class="map-container"></div>
    <div v-if="marker">
      <p>经度:{{ marker.lng }}</p>
      <p>纬度:{{ marker.lat }}</p>
      <p>地址:{{ marker.address }}</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      keyword: '',
      map: null,
      marker: null
    };
  },
  mounted() {
    // 加载地图
    this.loadMap();

    // 监听地图点击事件
    this.map.addEventListener("click", (e) => {
      this.handleMapClick(e.point);
    });
  },
  methods: {
    loadMap() {
      this.map = new BMap.Map(this.$refs.mapContainer);
      this.map.centerAndZoom(new BMap.Point(116.404, 39.915), 13);

      const navigationControl = new BMap.NavigationControl();
      this.map.addControl(navigationControl);
    },
    search() {
      if (!this.keyword) return;

      // 创建LocalSearch实例
      const localSearch = new BMap.LocalSearch(this.map, {
        onSearchComplete: (results) => {
          if (localSearch.getStatus() === BMAP_STATUS_SUCCESS) {
            const poi = results.getPoi(0);

            // 清除之前的标记
            if (this.marker) {
              this.map.removeOverlay(this.marker);
            }

            // 创建新的标记
            const marker = new BMap.Marker(poi.point);
            this.map.addOverlay(marker);
            this.marker = {
              lng: poi.point.lng,
              lat: poi.point.lat,
              address: poi.address
            };
          }
        }
      });

      // 发起关键字搜索请求
      localSearch.search(this.keyword.trim());
    },
    handleMapClick(point) {
      // 清除之前的标记
      if (this.marker) {
        this.map.removeOverlay(this.marker);
      }

      // 创建新的标记
      const marker = new BMap.Marker(point);
      this.map.addOverlay(marker);
      this.marker = {
        lng: point.lng,
        lat: point.lat,
        address: ''
      };

      // 根据经纬度获取地址
      const geoc = new BMap.Geocoder();
      geoc.getLocation(point, (result) => {
        if (geoc.getStatus() === BMAP_STATUS_SUCCESS) {
          this.marker.address = result.address;
        }
      });
    }
  }
};
</script>

<style scoped>
.map-container {
  width: 100%;
  height: 400px;
}
</style>```


## 三、vue文件中使用

```javascript
<template>
  <div>
    <map/>
  </div>
</template>

<script>
import map from './map.vue';

export default {
  components: {
    map
  }
};
</script>
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葫芦娃y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值