vue-baidu-map搜索地点

一、效果图
在这里插入图片描述

二、代码

<template>
  <div class="app-container">
    <el-autocomplete
      v-model.trim="address"
      :fetch-suggestions="querySearch"
      placeholder="请输入详细地址"
      style="width: 100%"
      :trigger-on-focus="false"
      @select="handleSelect"
    />
    <div style="margin: 5px">
      <baidu-map
        class="bm-view"
        :center="mapCenter"
        :zoom="mapZoom"
        @click="paintPolyline"
        :scroll-wheel-zoom="true"
        ak="baidu-ak"
        @ready="handlerBMap"
      >
        <bm-marker :position="mapCenter"> </bm-marker>
      </baidu-map>
    </div>
  </div>
</template>

<script>
import BaiduMap from "vue-baidu-map/components/map/Map.vue";
export default {
  name: "Map",
  components: {
    BaiduMap,
  },
  
  data() {
    return {
      mapZoom: 17,
      mapCenter: { lng: 117.233441, lat: 31.826885 },
    
      coordinate: "",
      address: "",
    };
  },
  created() {
    //console.log(this.longitude, this.latitude, 2);
  },
  methods: {
    async handlerBMap({ BMap, map }) {
      this.BMap = BMap;
      this.map = map;
      map.disableDoubleClickZoom() //禁止双击地图放大
      //禁止双指缩放disablePinchToZoom()
      // map.disableDoubleClickZoom() //禁止缩放
     //map.enableScrollWheelZoom(false); //开启鼠标滚轮缩放
     	map.disableDragging();     //禁止拖拽
      if (this.coordinate && this.coordinate.lng) {
        this.mapCenter.lng = this.longitude;
        this.mapCenter.lat = this.latitude;
        this.mapZoom = 17;
        map.addOverlay(new this.BMap.Marker(this.coordinate));
      } else {
        this.mapCenter.lng = this.longitude || 117.233441;
        this.mapCenter.lat = this.latitude || 31.826885;
        this.mapZoom = 17;
      }
    },
    querySearch(queryString, cb) {
      var that = this;
      var myGeo = new this.BMap.Geocoder();
      myGeo.getPoint(
        queryString,
        function(point) {
          if (point) {
            that.coordinate = point;
            that.makerCenter(point);
          } else {
            that.coordinate = null;
          }
        },
        this.locationCity
      );
      var options = {
        onSearchComplete: function(results) {
          if (local.getStatus() === 0) {
            // 判断状态是否正确
            var s = [];
            for (var i = 0; i < results.getCurrentNumPois(); i++) {
              var x = results.getPoi(i);
              var item = { value: x.address + x.title, point: x.point };
              s.push(item);
              cb(s);
            }
          } else {
            cb();
          }
        },
      };
      var local = new this.BMap.LocalSearch(this.map, options);
      local.search(queryString);
    },
    paintPolyline(item) {
      var { point } = item;
      this.flag = true;
      this.makerCenter(point);
    },
    handleSelect(item) {
      var { point } = item;
      this.coordinate = point;
      //console.log(point);
      this.makerCenter(point);
    },
    makerCenter(point) {
      if (this.map) {
        this.map.clearOverlays();
        this.map.addOverlay(new this.BMap.Marker(point));
        this.mapCenter.lng = point.lng;
        this.mapCenter.lat = point.lat;
        this.mapZoom = 17;
        this.mapCenter= { lng: point.lng, lat: point.lat }
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.bm-view {
  width: 100%;
  height: 300px;
}
</style>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值