element-ui 结合百度地图标记点位

element-ui 结合百度地图标记点位

引入api

import BaiduMap from "vue-baidu-map";

利用el-dialog

<el-dialog
      title="地图"
      :visible.sync="showMap"
      width="50%"
      :append-to-body="true"
      @closed="closeMap"
      style="height: 800px; overflow: hidden"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div v-if="showMap">
        <el-input
          class="addressInput"
          v-model="addressKeyword"
          placeholder="请输入地址来查找相关位置"
        ></el-input>
        <div class="control-btn">
          <el-button type="primary" @click="checkedAddress">确 定</el-button>
          <!-- <el-button @click="closeMap">关 闭</el-button> -->
        </div>
        <baidu-map
          :zoom="zoom"
          @ready="handler"
          style="height: 1080px"
          @click="getClickInfo"
          :scroll-wheel-zoom="true"
          v-loading="loadingMap"
        >
          <bm-view style="width: 100%; height: 500px; flex: 1"></bm-view>
          <bm-local-search
            :keyword="addressKeyword"
            :auto-viewport="true"
            style="display: none"
          ></bm-local-search>
        </baidu-map>
      </div>
    </el-dialog>

定义data相关属性

// 地图相关
      loadingMap: true,
      BMap: "",
      map: "",
      geoc: "",
      showMap: false,
      addressKeyword: "",
      longitude: "",
      latitude: "",
      pointLngLat: "",
      center: { lng: 109.45744048529967, lat: 36.49771311230842 },
      zoom: 18,

地图相关代码

//获取地图信息
    checkedAddress() {
      //地址
      this.form.addrDetail = this.addressKeyword;
      //经纬度
      this.form.addrLongitude = this.longitude;
      this.form.addrLatitude = this.latitude;
      this.showMap = false;
    },

    // 地图相关
    mapOpen() {
      this.top = this.getScrollTop();
      if (this.top) {
        this.setScrollTop(0);
      }
    },
    // 关闭地图后调用
    mapClose() {
      this.setScrollTop(this.top);
      this.top = 0;
      this.showMap = false;
    },
    getScrollTop() {
      let scrollTop = 0;
      if (document.documentElement && document.documentElement.scrollTop) {
        scrollTop = document.documentElement.scrollTop;
      } else if (document.body) {
        scrollTop = document.body.scrollTop;
      }
      return scrollTop;
    },
    setScrollTop(top) {
      if (!isNaN(top)) {
        if (
          document.documentElement &&
          document.documentElement.scrollTop !== undefined
        ) {
          document.documentElement.scrollTop = top;
        } else if (document.body) {
          document.body.scrollTop = top;
        }
      }
    },
    handler({ BMap, map }) {
      this.mapOpen();
      this.BMap = BMap;
      this.map = map;
      this.loadingMap = true;
      var geolocation = new BMap.Geolocation();
      this.geoc = new BMap.Geocoder(); // 地址解析对象
      var $this = this;
      // 调用百度地图api 中的获取当前位置接口
      geolocation.getCurrentPosition(function (r) {
        if ($this.longitude != "" && $this.latitude != "") {
          r.point.lng = $this.longitude;
          r.point.lat = $this.latitude;
        }
        let myGeo = new BMap.Geocoder();
        myGeo.getLocation(
          new BMap.Point(r.point.lng, r.point.lat),
          function (result) {
            if (result) {
              $this.loadingMap = false;
              // console.log(result);
              $this.$set($this, "pointLngLat", {
                lng: result.point.lng,
                lat: result.point.lat,
              });
              map.enableScrollWheelZoom(false); // 开启鼠标滚轮缩放,默认关闭
              // $this.addPoint({ BMap, map });
              map.clearOverlays();
              map.centerAndZoom(
                new BMap.Point(r.point.lng, r.point.lat),
                $this.zoom
              );
              window.map.addOverlay(
                new BMap.Marker(new BMap.Point(r.point.lng, r.point.lat))
              );
            }
          }
        );
      });
      window.map = map;
    },
    getClickInfo(e) {
      let _this = this;
      var myMarker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));
      var gc = new BMap.Geocoder();
      var point = new BMap.Point(e.point.lng, e.point.lat);
      gc.getLocation(point, (res) => {
        _this.addressKeyword = res.address;
        _this.longitude = point.lng;
        _this.latitude = point.lat;
        console.log(_this.addressKeyword); // 地址信息
        var mes =
          "确定位置在:" +
          "经度:" +
          _this.longitude +
          +"  " +
          "维度:" +
          _this.latitude +
          "  " +
          _this.addressKeyword +
          "吗?";
        MessageBox.confirm(mes, "提示", {
          showCancelButton: true,
          confirmButtonText: "确定",
          cancelButtonClass: "取消",
          type: "warning",
        })
          .then(() => {
            window.map.clearOverlays();
            window.map.centerAndZoom(
              new BMap.Point(_this.longitude, _this.latitude),
              _this.zoom
            );
            window.map.addOverlay(myMarker);
          })
          .catch(() => {});
      });
      _this.longitude = e.point.lng;
      _this.latitude = e.point.lat;
    },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值