Home  >  Q&A  >  body text

javascript - wx.getLocation无法正确执行回调,估计是作用域的原因

wx.getLocation({
    type: 'wgs84', 
    success: function (res) {
      alert("=======" + res.latitude);
      coords.latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
      coords.longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
      alert("com here===" + coords.longitude);
      getShopList();
    },
    cancel: function (res) {
      getShopList();
    },
    fail: function (res) {
      getShopList();
    }
  });
  
  
  上面是我写的调用方法,能进success方法,并且执行了第一个alert,但是下面的不能执行了。我怀疑是js不识别coords和getShopList方法,这我都在外层定义的,我怀疑是作用域的问题。但是不知道怎么解决
高洛峰高洛峰2655 days ago465

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-11 10:55:40

    coords和getShopList方法 的代码贴出来看看
    应该是 coords.latitude = res.latitude; 这里报错了导致执行中断了

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-11 10:55:40

    估计是没获取到coords.
    (function(co){wx.getLocation({})})(coords);
    把coords 传入闭包中.

    reply
    0
  • Cancelreply