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")
}
});
}