vue 百度地图获取经纬度和地址插件

1 篇文章 0 订阅

1、先看效果图

在这里插入图片描述

2、代码实现(组件)

npm install --save vue-baidu-map
2-1、compons文件夹下新建baiduMap.vue,内容:

<template>
  <div>
    <el-dialog
      width="30%"
      :before-close="cancel"
      :closable="false"
      :mask-closable="false"
      :visible="visible"
    >
      <span>
        <baidu-map
          v-bind:style="mapStyle"
          class="bm-view"
          ak="G1i75yz8WZEz871mIscsk4NTplkSakWB"
          :center="center"
          :zoom="zoom"
          :scroll-wheel-zoom="true"
          @click="getClickInfo"
          @moving="syncCenterAndZoom"
          @moveend="syncCenterAndZoom"
          @zoomend="syncCenterAndZoom"
        >
          <bm-view style="width: 100%; height:500px;"></bm-view>
          <bm-marker
            :position="{ lng: center.lng, lat: center.lat }"
            :dragging="true"
            animation="BMAP_ANIMATION_BOUNCE"
          >
          </bm-marker>
          <bm-control :offset="{ width: '10px', height: '10px' }">
            <bm-auto-complete v-model="keyword" :sugStyle="{ zIndex: 999999 }">
              <input
                type="text"
                placeholder="请输入搜索关键字"
                class="serachinput"
              />
            </bm-auto-complete>
          </bm-control>
          <bm-local-search
            :keyword="keyword"
            :auto-viewport="true"
            style="width:0px;height:0px;overflow: hidden;"
          ></bm-local-search> </baidu-map
      ></span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  BaiduMap,
  BmControl,
  BmView,
  BmAutoComplete,
  BmLocalSearch,
  BmMarker
} from 'vue-baidu-map'
export default {
  components: {
    BaiduMap,
    BmControl,
    BmView,
    BmAutoComplete,
    BmLocalSearch,
    BmMarker
  },
  data () {
    return {
      showMapComponent: this.value,
      keyword: '',
      mapStyle: {
        width: '100%',
        height: this.mapHeight + 'px'
      },
      center: { lng: 104.710326, lat: 27.118777 },
      zoom: 15
    }
  },
  props: {
    visible: Boolean,
    value: {
      type: Boolean,
      default: false
    },
    mapHeight: {
      type: Number,
      default: 500
    }
  },
  created () {},
  watch: {
    // value: function (currentValue) {
    //   this.showMapComponent = currentValue
    //   if (currentValue) {
    //     this.keyword = ''
    //   }
    // }
  },
  methods: {
    /***
     * 地图点击事件。
     */
    getClickInfo (e) {
      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()
    },
    /***
     * 确认
     */
    confirm: function () {
      this.$emit('map-confirm', this.center)
    },
    /***
     * 取消
     */
    cancel: function () {
      this.$emit('cancel')
    }
  }
}
</script>

<style lang="scss" scoped>
.serachinput {
  width: 300px;
  box-sizing: border-box;
  padding: 9px;
  border: 1px solid #dddee1;
  line-height: 20px;
  font-size: 16px;
  height: 38px;
  color: #333;
  position: relative;
  border-radius: 4px;
  -webkit-box-shadow: #666 0px 0px 10px;
  -moz-box-shadow: #666 0px 0px 10px;
  box-shadow: #666 0px 0px 10px;
}
</style>

2-2、页面引入并注册组件

<script>
	import baiduMap from '../../components/baiduMap'
	export default {
  		components: { baiduMap },
  		data () {
  			return {
  		  	  showMapComponent: false
  		  	}
	    }
  },
  
</script>

2-3、页面书写

<template>
<div>
	<baiduMap
      @cancel="cancelMap"
      @map-confirm="confirmMap"
      :visible="showMapComponent"
    	></baiduMap>
  </div>
</template>

2-4、方法

cancelMap () {
  this.showMapComponent = !this.showMapComponent
},
 confirmMap (e) {
   console.log(e)
   	// const obj = { 经度: e.lng, 纬度: e.lat }
  	// const arr = []
   	// for (var k in obj) {
   	//   const str = k + ':' + obj[k]
   	//   arr.push(str)
   	// }
   	// this.form.longitudeLatitude = arr.join(',')
   	// this.form.longitude = e.lng + ''
   	// this.form.dimension = e.lat + ''
   this.showMapComponent = !this.showMapComponent
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值