后台管理百度地图-获取位置信息

发布时间:2023年12月25日

?1.在输入框中输入位置获取经纬度,拿到经纬度用定点标记在地图上

2.鼠标点击地图中的位置,调用组件点击事件拿到经纬度用定点标记在地图上,删除之前的定点标记,拿到的地址赋值到输入框中,

?

?引入百度地图:?<script type="text/javascript" src="https://api.map.baidu.com/api?v=1.0&type=webgl&ak=你的ak"></script>

<el-input v-model="address"></el-input>
<el-button type="primary" @click="mapSubmit()">确定</el-button>
<div style="height: 380px;">
     <div id="container" style="height: 100%; width: 100%;"></div>
</div>
  data() {
    return {
      map: {},
      point: {},
      marker: {},
      geocoder: {},
      address:''
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.map = new BMapGL.Map('container')
      var zoomCtrl = new BMapGL.ZoomControl()
      this.map.addControl(zoomCtrl)
      this.geocoder = new BMapGL.Geocoder()
      //点击地图获取的回调函数
      this.map.addEventListener('click', (e) => {
        this.addPoint(e.latlng, true)
      })
    })
  },
  methods: {
    //封装一个方法,传入两个参数p:经纬度,center:布尔-设置地图的中心点和缩放级别
    addPoint(p, center) {
      this.map.removeOverlay(this.marker)//删除上次定点标记
      let lng = p.lng
      let lat = p.lat
      this.point = new BMapGL.Point(lng, lat)
      this.marker = new BMapGL.Marker(this.point)
      this.map.addOverlay(this.marker)
      this.addGeocoder(this.marker.point)
      if (center) {
        this.map.centerAndZoom(this.point, 18)
      }
    },
    //根据经纬度获取位置信息
    addGeocoder(point) {
      this.geocoder.getLocation(this.point, (point) => {
        this.address = point.address
      })
    },
}

回显:?this.point = new BMapGL.Point(lng, lat); this.addPoint(this.point, true)

输入地址点击确定获取经纬度和定点 :

   mapSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const geocoder = new BMapGL.Geocoder()
          geocoder.getPoint(this.address, (point) => {
            if (point) {
              this.addPoint(point, true)
            } else {
              alert('地址解析失败,请输入有效的地址!')
            }
          })
        } else {
          return this.$message.error('你还有信息没有填写')
        }
      })
    },

文章来源:https://blog.csdn.net/weixin_58431406/article/details/135191589
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。