关于getLocation
发布于 6 年前 作者 ryao 4742 次浏览 来自 问答

有时候需要同时获取wgs84和gcj02两种坐标,但用如下的方式,连续两次调用getLocation,始终得到相同的坐标

wx.getLocation({

  type: 'wgs84',
  success: function(res){
     that.setData({loc: res});
  }
});
wx.getLocation({
  type: 'gcj02',
  success: function (res) {
    that.setData({ loc2: res });
  }
});

用下面的方式,在一个getLocation的success回调中再次调用getLocation,才可以得到两种不同的坐标

var that = this;
wx.getLocation({
    type: 'wgs84',
    success: function(res){
        that.setData({loc: res});
        wx.getLocation({
            type: 'gcj02',
            success: function (res) {
                that.setData({loc2:res });
            }
        });
    }
});

以上都要调用两次getLocation,效率太低,建议只调用一次getLocation,就能同时获得wgs84和gcj02两种坐标

2 回复

如果调用两次getLocation,很多东西都要重复一次。其实gcj02就是由wgs84计算而来的,在getLocation中增加一个参数,决定是否计算gcj02,success返回参数再增加两个经纬度字段,保存gcj02经纬度,如果没有计算,这两个字段值就为null,原有的latitude和longitude就保存wgs84坐标,其他的高度、精度之类的,其实都一样。

回到顶部