微信公衆號 獲取用戶地理位置

微信公衆號可以在剛進去頁面的時候調取獲取地理位置的接口,js代碼如下

<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>

function position(){
        var url=window.location.href;
        if(uid != null){
            //更新定位
            $.ajax({
                type: "post",
                url: "/wechat/Wechatlogin/getinfo",
                dataType: 'json',
                data:{url:url},
                success: function(result) {
                    console.log(result, '值')
                    if(result != null){
                        getposition(result);
                    }
                }
            });
        }
    }
//獲取地理位置
    function getposition(param){
        wx.config({
            debug: false,  //調式模式,設置爲ture後會直接在網頁上彈出調試信息,用於排查問題
            appId: param["appId"],
            timestamp: param["timestamp"],
            nonceStr: param["nonceStr"],
            signature: param["signature"],
            jsApiList: [ // 所有要調用的 API 都要加到這個列表中
                'checkJsApi',
                'openLocation',
                'getLocation'

            ]
        });
        wx.ready(function () {

            wx.checkJsApi({
                jsApiList: [
                    'getLocation',
                    'translateVoice'
                ],
                success: function (res) {
                    // alert(JSON.stringify(res));
                    // alert(JSON.stringify(res.checkResult.getLocation));
                    if (res.checkResult.getLocation == false) {
                        alert('你的微信版本太低,不支持微信JS接口,請升級到最新的微信版本!');
                        return;
                    }
                }
            });
            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; // 位置精度

                    //請求更新位置信息的接口
                    $.ajax({
                        type: "post",
                        url: "http://jmbj.zhangtongdongli.com/wechat/wechatlogin/updateposition",
                        dataType: 'json',
                        data:{'lat':latitude,'log':longitude,'uid':uid},
                        success: function(result) {
                            //alert(result);
                            console.log(result, '更新數據')

                        }
                    });
                    //window.location.href="http://jimaobj.zhangtongdongli.com/xidan.html?id=1&lat="+latitude+"&log="+longitude;
                    //alert(latitude+','+longitude+','+accuracy);
                },
                cancel: function (res) {
                    alert('用戶拒絕授權獲取地理位置');
                }
            });

        });
        wx.error(function (res) {
            //alert(res.errMsg);  //打印錯誤消息。及把 debug:false,設置爲debug:ture就可以直接在網頁上看到彈出的錯誤提示

        });
    }

</script>

/wechat/Wechatlogin/getinfo 這個接口就是返回需要的參數

昨天我改了一個公衆號,發現調用地址的接口壞了,搞了1個小時終於成功了,一直報簽名錯誤,把白名單加上就可以了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章