微信jssdk循环获取地理位置接口,位置信息为什么不一致

移动开发 码拜 8年前 (2016-04-12) 1120次浏览
var gps = new array();
function getLocation() {
    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; // 位置精度
            gps.push({"lat":latitude, "lng":longtitude});
            //alert("xx");
        },
        fail:function(){
            gps.push("error")
        }
    });
}
setInterval("getLocation()", 15000);

实现用户在运动的过程中,通过坐标,计算运动的距离,可是除了第一个点,中间点都是error,但假如将alert的注释去掉,那么每次都能获取到坐标点,是需要用户触发才能获取坐标?用户不触发,就全部返回失败?

解决方案

40

这个接口是异步的  显然是不能这么调用

var gps = new array();
function getLocation() {
    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; // 位置精度
            gps.push({"lat":latitude, "lng":longtitude});
            setTimeout("function(){ getLocation(); }",15000);   
            //alert("xx");
        },
        fail:function(){
            gps.push("error")
        }
    });
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明微信jssdk循环获取地理位置接口,位置信息为什么不一致
喜欢 (0)
[1034331897@qq.com]
分享 (0)