關於微信公衆號 微信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("未能獲取地理位置");
            }
          });
        });
      };
相關文章
相關標籤/搜索