在vue中使用高德地图的拖拽选址

在vue中使用高德地图的拖拽选址


1. webpack引入高德

webpack.base.conf.js中找到module.exports对象。添加externals: { 'AMap': 'AMap' }代码。

2. index.html引入高德js

index.html页面加入<script src="https://webapi.amap.com/maps?v=1.4.15&key=你的K值"></script>
<script src="http://webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>

3. 在页面中引入高德js

在vue文件中import进来。import AMap from 'AMap'; //在页面中引入高德地图

4.data中的数据:

data () {
    return {
      lng: '',
      lat: '',
      address: '',
      initEdit: false,
      lngLat: '',
      fullAdress: '天津市西青区', // 可填写默认地址,下边填写默认的坐标
      district: '',
      province: ''
    }
  }

5.methods绘制地图代码:

  initMap () {
      this.fullAdress = '天津市西青区'
      this.initEdit = false
      let AMapUI = window.AMapUI
      let that = this
      AMapUI.loadUI(['misc/PositionPicker'], function (PositionPicker) {
        var map = new AMap.Map('container', {
          resizeEnable: true,
          scrollWheel: true,
          zoom: 13
        })
        AMap.plugin(['AMap.ToolBar'], function () {
          map.addControl(new AMap.ToolBar({
            liteStyle: true
          }))
        })
        var positionPicker = new PositionPicker({
          mode: 'dragMap',
          map: map
        })
        positionPicker.on('success', function (positionResult) {
          if (this.initEdit === false) {
            let geocode = positionResult.regeocode
            that.lngLat = positionResult.position.getLng() + ' , ' + positionResult.position.getLat()
            that.fullAdress = positionResult.address
            this.address = {
              lng: positionResult.position.getLng(),
              lat: positionResult.position.getLat(),
              sheng: geocode.addressComponent.province,
              shi: geocode.addressComponent.city,
              qu: geocode.addressComponent.district,
              jiedao: geocode.addressComponent.township,
              menpaihao: geocode.addressComponent.streetNumber,
              dizhiquanming: positionResult.address
            }
            if (geocode.addressComponent.province === '北京市') {
              this.address.shi = '北京市'
            }
            if (geocode.addressComponent.province === '天津市') {
              this.address.shi = '天津市'
            }
            if (geocode.addressComponent.province === '上海市') {
              this.address.shi = '上海市'
            }
            if (geocode.addressComponent.province === '重庆市') {
              this.address.shi = '重庆市'
            }
          }
          this.initEdit = false
        })
        positionPicker.on('fail', function (positionResult) {
          // 海上或海外无法获得地址信息
          that.lngLat = ''
          that.fullAdress = ''
        })
        if (that.lng === '' || that.lat === '') {
          that.lngLat = new AMap.LngLat('经度', '纬度') // 默认的经纬度
        } else {
          that.lngLat = new AMap.LngLat(that.lng, that.lat)
        }
        console.log(that.lngLat)
        positionPicker.start(that.lngLat)
      })
    }

6、最后加载一下:
根据自己需求加载出来

mounted () {
    this.initMap ()
  }

版权声明:本文为博主原创文章,转载请附上博文链接!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值