wx.config({ debug: true , // 因为在手机上测试没法打印,当debug为true时,所有的返回值都会在手机上alert出来 appId: sdkSign.appId, // 必填,公众号唯一标识 timestamp: sdkSign.timestamp, // 必填,生成签名的时间戳 nonceStr: sdkSign.nonceStr, // 必填,生成签名的随机串 signature: sdkSign.signature, // 必填,签名 jsApiList: [ 'getLocation' ,] // 必填,需要调用的接口列表,本需求里只是获取位置信息,具体的接口名称查阅js-sdk文档 }); //获取地理位置 wx.ready( function (){ wx.getLocation({ type: 'wgs84' , // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02' success: function (res) { var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90 var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。 console.log( '经度:' +longitude); console.log( '纬度:' +latitude); getLocationBylatlng(latitude, longitude); }, cancel: function (res) { alertMsg( "定位失败" , 2); }, fail: function (res){ alertMsg( "定位失败" , 2); } }); }); |
您好,请在complete里面加日志,确定消息走到了这里。
如果还是有问题,请提供代码片段。
我也碰到同样的问题,部分安卓机的部分时刻,可以看见右上角的定位状态一直在激活中,但是没有执行wx.getLocation()的回调。
建议wx.getLocation()加上超时控制的处理…不然强依赖wx.getLocation()的话,没有回调整段代码都hang住了~
你好,请问你是怎么解决定位一直在激活状态不进行回调这个问题的?
我就是在用户已授权地理位置后,人为给了一个超时设置~供你参考~
const LocationAuth =
'scope.userLocation'
;
const getAuthSettingPromise =
new
Promise((resolve, reject) => {
wx.getSetting({
success(res) {
return
resolve(res.authSetting || {});
},
fail: reject
});
});
export const getLocation = () => {
return
getAuthSettingPromise.then((auths) => {
return
new
Promise((resolve, reject) => {
const timeOut = setTimeout(() => {
if
(auths[LocationAuth] !== undefined) {
reject({errMsg:
'调用wx.getLocation超时'
, isError:
true
});
},
4000);
wx.getLocation({
type:
'wgs84'
,
success(res) {
clearTimeout(timeOut);
resolve(res);
},
fail(res) {
clearTimeout(timeOut);
reject(res);
}
});
});
});
};
好的 谢谢啦