在旅行社做网站运营,静态网站开发的目的,恒美广告公司,桂林最新情况最新消息今天uni.getlocation 在H5中#xff0c;如果用户未开gps定位或者gps定位信号较差时#xff0c;定位会失败。这种情况uni.getlocation也不会出现报错#xff0c;也不会有后续执行#xff0c;导致代码阻塞#xff0c;体验极差。 解决方案1#xff1a;拿不到定位或者定位失败这个… uni.getlocation 在H5中如果用户未开gps定位或者gps定位信号较差时定位会失败。这种情况uni.getlocation也不会出现报错也不会有后续执行导致代码阻塞体验极差。 解决方案1拿不到定位或者定位失败这个时候可以尝试与后端配合使用ip定位进行定位保证后续的执行。 解决方案2引导用户开启gps定位可以直接跳转到手机gps定位界面为最好目前没有找到合适的方法如果您知道得话欢迎留言讨论。 解决方案3前端对uni.getLocation进行处理。因为未开定位时uni.getlocation根本不执行所以在uni.getlocation中不管是success、fail、compelet都不会执行这个时候我们可以添加一个宏任务。如果uni.getlocation未执行就可以通过setTimeout的执行进行返回如果uni.getLocation执行了就直接通过uni.getlocation进行返回。代码如下
/*** description 通过uni.getLocation获取相关地理信息* returns */
export function getLatLng() {return new Promise((resolve) {uni.getLocation({type: wgs84,timeout: 6,fail: () {resolve({...getLocationLatLng(), errMsg: getLocation:fail})},success: (res) {const params { ...res, latitude: res.latitude, longitude: res.longitude }uni.setStorageSync(LatLng, JSON.stringify(params));resolve(params);},})const timer setTimeout(() {clearTimeout(timer)resolve({...getLocationLatLng(), errMsg: getLocation:fail})}, 6000)})
}