首页 > 其他分享 >uniapp获取定位导致APP闪退

uniapp获取定位导致APP闪退

时间:2024-03-21 13:45:08浏览次数:23  
标签:uniapp res APP delay wv let uni 闪退 setVersion

刚开始用setInterval定时1秒获取地图定位,因为uni.getLocation经常会返回失败,导致整个定时器错乱闪屏崩溃。

本页面有一个webview,通过evalJS执行webview嵌套页面的方法。

<web-view @message="getMessage" v-if="url" :src="url"></web-view>

 

错误代码:

this.time = setInterval(() => { 
  if(!this.wv){
    let wv = currentWebview.children()[0];
    this.wv = wv; 
  }
  // console.log(currentWebview,"currentWebview---------------111",this.wv)
  this.setVersion();  
}, 1000);  

 setVersion() {
      uni.getLocation({
          type: 'gcj02',
          altitude: true,
          success:(res)=> {
            // longitude:res.longitude,
            // latitude:res.latitude,
            this.wv.evalJS("setVersion('" + JSON.stringify(res) + "')");  
          }
      });
    },  

 

优化后代码,setInterval替换成setTimeout,根据uni.getNetworkType返回网络正常标识来决定间隔时间。

setTimeout(() => {
  this.startgetLocation()
}, 50)

// 获取定位信息
async startgetLocation() {  
  let that = this;  
  let delay = 3000;//定时器时间间隔3s
  let delay_seek = 15000;//如果网络异常,增加定时器时间间隔15s  
  let isNetCon = true;//网络正常
  
  uni.getNetworkType({
    success: res => {
      if (res.networkType === 'none') {
        isNetCon = false;
      }
    }
  }); 
   
  if (isNetCon) {  
    try {  
      await that.setVersion();//调取定位接口  
    } catch (e) {  
      delay = delay + delay_seek;  
    }  
  } else {  
    delay = delay + delay_seek;  
  }  

if (this.time)  clearTimeout(this.time);//清除定时器
  this.time = setTimeout(function() {  
    that.startgetLocation();  
  }, delay);  
},
//调取定位接口  
setVersion() {
  let that = this;
  return new Promise((resolve, reject) => { 
    uni.getLocation({
      type: 'gcj02',//默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
      altitude: true,
      geocode: true,
      // highAccuracyExpireTime: 100, // 需要设置highAccuracyExpireTime,在规定时间内返回当前最高精度的经纬度
      success:(res)=> {
        if(!that.wv){
          let currentWebview = that.$scope.$getAppWebview();
          let wv = currentWebview.children()[0];
          that.wv = wv;
        }

        if(that.wv){
          that.wv.evalJS("setVersion('" + JSON.stringify(res) + "')");
        }
        resolve('suc');  
      },
      fail: function(err) {  //定位失败 
          // console.log('获取失败') 
        reject('err');  
      }  
    });
  }) 
},

  

 

   

翻译

搜索

复制

标签:uniapp,res,APP,delay,wv,let,uni,闪退,setVersion
From: https://www.cnblogs.com/zoushuangyu/p/18087181

相关文章

  • 使用appuploder流程笔记
     1.如何没有账号去apple官网注册一个,地址:https://developer.apple.com/account2.下载解压appuploder,双击打开,用刚刚注册的账号登录,下载地址:http://www.applicationloader.net/(使用第一次后,可以点击记住密码即可一键登录)注意:未支付apple的账号需要勾选“未付苹果688”  ......
  • uni-app攻略:如何对接驰腾打印机
    一.引言在当前的移动开发生态中,跨平台框架如uni-app因其高效、灵活的特点受到了开发者们的青睐。同时,随着物联网技术的飞速发展,智能打印设备已成为许多业务场景中不可或缺的一环。今天,我们就来探讨如何使用uni-app轻松对接驰腾品牌的智能打印机,实现无线打印功能。无论您是初学者......
  • 【基于stm32-心率血氧量检测系统(蓝牙,手机app)】
    一、项目描述:心率血氧量检测系统基于STM32芯片的设计将为用户提供高效、可靠的健康监测解决方案。以下是这一系统的主要特点和功能:STM32芯片驱动:采用STM32系列芯片作为主控制器,具有高性能和低功耗特性,能够确保系统稳定运行并延长电池寿命。传感器集成:整合了高精度的心率和血......
  • uniapp 开发模板
    简介vue3-uniapp-template是基于vue3的uniapp快速开发模板,包含状态管理、网络请求、路由拦截、UI组件等常用功能。主要使用的技术栈:uniapp、vue3、pinia、vite、uv-ui下载地址PS:如果对你有帮助的话,点个Star支持下哈~GithubGitee项目启动#克隆代码gitclonehttps://gi......
  • Application.Current.Dispatcher.Invoke VS Dispatcher.CurrentDispatcher.Invoke
    写WPF的多线程一定离不开这两个东西,但是一些偶尔中发现项目中同事功能我多线程调用的时候发现线程安全问题,一查,他们都把核心操作都放在了UI线程中操作呀还是全局大范围的直接放Application.Current.Dispatcher.Invoke,但是依然出现,细查后发现,有的人使用的是Application.Current.......
  • ACCESS 关于使用VBA选择路径时提示"方法'FileDislog作用于对象'_Application’时失败"
    以下是源码:PrivateSubCommand0_Click()'打开文件选择对话框WithApplication.FileDialog(msoFileDialogFilePicker).AllowMultiSelect=False.Filters.Clear.Filters.Add"Excel文件","*.xls;*.xlsx",1I......
  • how BabyFile app transfer files with mac
    asfollows: 1.ConnecttheiPhone/iPadwithadatacable,andthenopentheMac'sFinder,asmarkedby①inthepicture.(ifyourMaccomputerwithaparticularlylowsystemversion,openiTunes)2.Findandclicktheconnecteddeviceintheleft......
  • AutoMapper
    AutoMapper是一个在.NET应用程序中自动将一个对象的属性映射到另一个对象的属性的开源库。它旨在减少代码的重复性和提高开发人员的生产力。在实际应用中,我们常常需要将一个实体对象转换为另一个实体对象,或者从数据访问层获取到的数据对象映射到业务逻辑层的对象。手动实现这......
  • spring refresh的流程(AbstractApplicationContext的refresh方法)
    12个阶段1、prepareRefresh,做准备工作2、obtainFreshBeanFactory,创建或获取beanfactory3、prepareBeanFactory,准备beanfactory4、postProcessBeanFactory,子类扩展beanfactory5、invokeBeanFactoryPostProcessors,后处理器扩展beanfactory6、registerBeanPostProcessors,准备b......
  • ASP.NET通过Appliaction和Session统计在人数和历史访问量
    目录背景:Appliaction:Session:过程:数据库:Application_Start:Session_Start:Session_End:Application_End:背景:事件何时激发Application_Start在调用当前应用程序目录(或子目录)的第一个ASP.NET页面时激发Applicaiotn_End在应用程序最后一次会话结束时激发,此外在使用I......