antd vue 百度地图选点

antd vue 百度地图选点

在这里插入图片描述
点击地图选点按钮,弹出页面选择地址,选择完成后,详细地址会自动变更,详细地址可以编辑
在这里插入图片描述
BMapComponent.vue

<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
<a-spin :spinning="confirmLoading">
    <div id="allmap" class="mapbox"> 
      <baidu-map
        :center="center"
        :zoom="zoom"
        :scroll-wheel-zoom="true"
        style="height:400px"
        @click="getClickInfo"
        @ready="handler"
        @moving="syncCenterAndZoom"
        @moveend="syncCenterAndZoom"
        @zoomend="syncCenterAndZoom">
        <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
        <bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT" :showAddressBar="true" :autoLocation="true"></bm-geolocation>
        <bm-marker :position="{lng: center.lng, lat: center.lat}" :dragging="false"></bm-marker>
      </baidu-map>
    </div>

</a-spin>
  </a-modal>
</template>

<script>
  export default {
    name: "BMapComponent",

    data () {
      return {
        title:"选择地址",
        visible: false,
        model: {},
        confirmLoading: false,
        headers:{},
        center:{
          lng:114.42818022698,
          lat:30.479137926147
        },
        zoom:15,
        storeLocation:{},
      }
    },

created () {
},
mounted() {
},
    watch:{
/*      center:{
        handler(newPoint,oldPoint){
          this.marker = {lng:newPoint.lng, lat:newPoint.lat};
        },
        deep: true,
        immediate: true
      }*/
    },
methods: {
  show(record) {
    this.visible = true;
    Object.assign(this.model, record);
    this.center.lng = record.lng;
    this.center.lat = record.lat;
    this.zoom = 15;
    this.$nextTick(() => {
      console.log(JSON.stringify(record))
    });

  },
  close () {
    this.$emit('close');
    this.visible = false;
  },

  handleOk() {
    const that = this;

    let point = new BMap.Point(this.center.lng, this.center.lat);
    let coder = new BMap.Geocoder();
    coder.getLocation(point,result => {
      this.storeLocation = {
        lng: point.lng,
        lat: point.lat,
        address: result.address
      }
      console.log("address="+result.address)
      console.log("storeLocation="+JSON.stringify(this.storeLocation));
      this.$emit('getStoreLocation', this.storeLocation);
      this.close();
    });

  },

  handleCancel () {
    this.close()
  },

  //地图
  handler({ BMap, map }) {
    console.log(BMap, map);
    map.centerAndZoom(new BMap.Point(this.model.lng,this.model.lat), 15);
  },
  getClickInfo(e) {
    console.log(e.point.lng)
    console.log(e.point.lat)
    this.center.lng = e.point.lng
    this.center.lat = e.point.lat
  },
  syncCenterAndZoom (e) {
    const {lng, lat} = e.target.getCenter()
    this.center.lng = lng
    this.center.lat = lat
    this.zoom = e.target.getZoom()
  },

    }
  }
</script>

<style lang="less" scoped>

</style>

父页面 部分代码

    <a-form :form="form">
    <a-form-item
      :labelCol="labelCol"
      :wrapperCol="wrapperCol"
      label="详细地址">
      <a-input-search placeholder="请输入详细地址" v-decorator="['address', {}]" @search="selectLocation" enterButton="地图选点" />
    </a-form-item>
    </form>

	<b-map-component ref="BMapComponent" @getStoreLocation="updateStoreLocation"></b-map-component>

import BMapComponent from './BMapComponent'

  export default {
    components:{  //用来注册子组件的节点
      "b-map-component": BMapComponent
    },
   
    // methods里面
  selectLocation(){
    let record = {
      lng: this.model.lng,
      lat: this.model.lat
    }
    this.$refs.BMapComponent.show(record)
  },

  updateStoreLocation(record){
    console.log("record="+JSON.stringify(record))
    this.model.lat = record.lat
    this.model.lng = record.lng;
    this.model.address = record.address;
    this.form.setFieldsValue({address:record.address});
  }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值