关于微信公众号 微信JSSDK 获取经纬度

代码以下:javascript

<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>//重点 引入这句html

<!-- <script type="text/javascript" src="https://res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/vconsole/2.5.1/vconsole.min.js"></script> -->//测试 引用java

 

function jsSdk() {
        var baseUrl = '***********';
            $.ajax({
          cache: false,
          url: baseUrl + "/api/boss/js_sign",
          type: "GET",
          data: 'URL=' + encodeURIComponent(location.href.split('#')[0]),
          dataType: "json",
          success: function (json) {
            if (json) {
              jsLocation(json)
            }
          },
          error: function (e, errorType, error) {
          },
        });
      };

   function jsLocation(params) {
        wx.config({
          beta: true,
          debug: false,
          appId: params.appId,
          timestamp: params.timestamp,
          nonceStr: params.nonceStr,
          signature: params.signature,
          jsApiList: ['checkJsApi', 'openLocation', 'getLocation'] // 必填,须要使用的JS接口列表
        });
        wx.ready(function () {
          wx.checkJsApi({
            jsApiList: ['getLocation'], // 须要检测的JS接口列表,全部JS接口列表见附录2,  
            success: function (res) {
              if (res.checkResult.getLocation == false) {
                alert("你的微信版本过低,不支持微信JS接口,请升级到最新的微信版本!");
                return;
              }
            }
          });
          var latitude = '';
          var longitude = '';
          var speed;
          var accuracy;
          wx.getLocation({
            success: function (res) {
              var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
              var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
              var speed = res.speed; // 速度,以米/每秒计
              var accuracy = res.accuracy; // 位置精度
              var baseUrl = '*********';
              //获取微信管理员信息
              var location = {
                "cafeId": window.cafeId,
                "gpsLng": longitude,
                "gpsLat": latitude,
              }
              $.ajax({
                cache: false,
                url: baseUrl + "/api/boss/update_cafe_location",
                type: "POST",
                contentType: "application/json",
                data: JSON.stringify(location),
                dataType: "json",
                success: function (json) {
                  if (json.success) {
                    alert("上传位置信息成功!")
                  } else {
                    alert("网络错误!")
                  }
                },
                error: function (e, errorType, error) {
                  alert("请求异常")
                },
              });
            },
            fail: function (e) {
              alert("未能获取地理位置信息");
            },
            cancel: function (res) {
              console.log('_____未能获取地理位置______');
              alert("未能获取地理位置");
            }
          });
        });
      };
相关文章
相关标签/搜索