admin 发表于 2024-5-26 13:12:12

微信jssdk定位

wx.ready(function(){
      var located = "{$_GPC['located']}";
      if(!located){
            getLocation();
      }
    });
    //JSSDK调用失败时触发
    wx.error(function(res){
      console.log(res)
    });
    //定位
    function getLocation() {
      wx.getLocation({
            // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
            type: 'gcj02',
            success: function (res) {
                var latitude = res.latitude;
                var longitude = res.longitude;
                var speed = res.speed;
                var accuracy = res.accuracy;
                var params = {
                  'latitude': latitude,
                  'longitude': longitude
                }
                localStorage.latitude = latitude;
                localStorage.longitude = longitude;
                $.post("{php echo $this->createMobileUrl('locate')}", params, function(res){
                  var data = $.parseJSON(res);
                  if(data.message.errno < 0){
                        $.alert(data.message.message, '温馨提示', function(){
                            window.location.href = data.redirect;
                        });
                  } else {
                        $.showLoading();
                        setTimeout(function() {
                            $.hideLoading();
                        }, 3000);
                        window.location.href = data.redirect;
                  }
                });
            }
      });
    }

页: [1]
查看完整版本: 微信jssdk定位