【vue-baidu-map】点击获取点坐标

点击获取坐标

需求

1.点击某点设置该点为中心点

2.获取点的经纬度

3.确定选取成功,取消就不赋值。 

实现

第一步:设置打开弹窗的地方

<el-button @click="clickAddress">项目坐标</el-button>

第二步:设置初始值

data() {
  return {
    center: { lng: 118.83, lat: 31.95 },
    zoom: 13,
    adr: '',
    dialogVisible: false,
  }
}

第三步:打开弹窗,将中心点清空,如果是编辑状态将经纬度赋值给弹窗中的经纬度。

clickAddress() {
  this.dialogVisible = true;
  this.adr = this.form.latLng
  let latLng = this.adr.split(',')
  this.center = {
    lng: latLng[0],
    lat: latLng[1],
  };
},

第四步:设置弹窗

getClickInfo 获取当前点击的经纬度,赋值中心点给marker。并且将当前经纬度显示出来

<bm-marker> 设置当前点击的位置marker点

<el-dialog title="项目坐标" :visible.sync="dialogVisible" width="50%">
  <baidu-map
    center="江宁"
    :zoom="zoom"
    @ready="handler"
    style="height: 600px"
    @click="getClickInfo"
    :scroll-wheel-zoom="true"
  >
    
    <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>

    <bm-marker
      :position="center"
      :dragging="true"
      animation="BMAP_ANIMATION_BOUNCE"
    >
    </bm-marker>

    <bm-geolocation
      anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
      :showAddressBar="true"
      :autoLocation="true"
    ></bm-geolocation>

  </baidu-map>

  <div class="latitude">经纬度:{{ latitude }}</div>

  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
</el-dialog>
handler({ BMap, map }) {
  console.log(BMap, map);
},

getClickInfo(e) {
  this.center.lng = e.point.lng;
  this.center.lat = e.point.lat;
  this.adr = e.point.lng + "," + e.point.lat;
},

 第五步:取消就直接关闭弹窗,如果确定就将弹窗中的经纬度赋值给表单中的form.latLng

dialogCancel() {
  this.dialogVisible = false;
},
dialogSubmit() {
  this.form.latLng = this.adr
  this.dialogVisible = false;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

宾果的救星

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

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

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

打赏作者

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

抵扣说明:

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

余额充值