testtesttesttesttesttesttesttesttesttesttest

主要是this指向的问题

1.在data里面先定义经纬度

data () {
    return {
      longit: '', // 经度
      latit: '',  // 纬度
    }
  }

2.在methods里面

  • 1.生成地图并获取当前位置的经纬度

getMap () {
    // 先定义that = this   
  var that = this   

  // 生成地图
  var map = new window.AMap.Map('container', {
    resizeEnable: true,
    zoom: 11,
    showIndoorMap: false, // 是否在有矢量底图的时候自动展示室内地图,PC默认true,移动端默认false
    dragEnable: true, // 地图是否可通过鼠标拖拽平移,默认为true
    keyboardEnable: false, // 地图是否可通过键盘控制,默认为true
    doubleClickZoom: true, // 地图是否可通过双击鼠标放大地图,默认为true
    zoomEnable: true, // 地图是否可缩放,默认值为true
    rotateEnable: false, // 地图是否可旋转,3D视图默认为true,2D视图默认false
    viewMode: '3D'
  })


  // 获取到当前位置的定位
  AMap.plugin('AMap.Geolocation', function() {
    var geolocation = new AMap.Geolocation({
    // 是否使用高精度定位,默认:true
    enableHighAccuracy: true,
    // 设置定位超时时间,默认:无穷大
    timeout: 10000,
    // 定位按钮的停靠位置的偏移量,默认:Pixel(10, 20)
    buttonOffset: new AMap.Pixel(10, 20),
    //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
    zoomToAccuracy: true,     
    //  定位按钮的排放位置,  RB表示右下
    buttonPosition: 'RB'
    })

    geolocation.getCurrentPosition()
    AMap.event.addListener(geolocation, 'complete', onComplete)
    AMap.event.addListener(geolocation, 'error', onError)

   
    function onComplete (data) {
      // data是具体的定位信息
      console.log('data是具体的定位信息', data)

      // 这里使用that而不是this,如果使用this,则无法在其他地方获取到这里的经纬度
      that.longit = data.position.lng
      that.latit = data.position.lat
    }
     function onError (data) {
       // 定位出错
       console.log('定位失败')
     }
  })
  that.maps = map
}
  • 2.拿到步骤1获取到的经纬度(methods里面),此处使用this而不是that,之前是因为在getMap ()方法里面定义了that = this,这里未定义,可以直接用this

 conf () {
  console.log('确定')
  console.log('666', this.longit, this.latit)
},

ps:conf ( )在一直拿不到getMap ( )里的经纬度,后来发现是getMap ( )里的this和AMap里的this指向不同,利用var that = this解决了这个问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值