ios端 plus.geolocation.watchPosition 参数 maximumAge 无效 在 uni-app 中

ios端 plus.geolocation.watchPosition 参数 maximumAge 无效 在 uni-app 中

项目 信息
产品分类 uniapp/App
PC开发环境操作系统 Windows
PC开发环境操作系统版本号 10
HBuilderX类型 Alpha
HBuilderX版本号 3.93
手机系统 iOS
手机系统版本号 iOS 15
手机厂商 苹果
手机机型 12
页面类型 vue
vue版本 vue2
打包方式 云端
项目创建方式 HBuilderX

示例代码:

let { amapLocation } = this.globalData;  
if(amapLocation) this.stopBackLocation();  
console.log("此处应当先云不行 12");  
amapLocation = plus.geolocation.watchPosition(  
    (position)=>{
        const { latitude, longitude } = position.coords;  
        console.log('定位成功',latitude, longitude, new Date());  
        if(fun) {  
            // fun(latitude, longitude, arge);  
        }  
    },  
    (e)=>{
        console.log('定位失败',e);  
    },  
    {  
        maximumAge: 1000 * 30, 
        provider: 'amap', 
        enableHighAccuracy: true,
        coordsType:"gcj02",
        geocode:false
    }  
)  
this.globalData.amapLocation = amapLocation;

操作步骤:

运行代码即可

预期结果:

预期结果30s打印一次

实际结果:

1s打印多次或几秒钟打印一次

bug描述:

plus.geolocation.watchPosition 设置 maximumAge 参数在ios端无效,1秒钟可能直接执行四五次


更多关于ios端 plus.geolocation.watchPosition 参数 maximumAge 无效 在 uni-app 中的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

你看下 这个属性后面的注释 maximumAge iOS平台根据设备位置变化自动计算回调更新的间隔时间

更多关于ios端 plus.geolocation.watchPosition 参数 maximumAge 无效 在 uni-app 中的实战教程也可以访问 https://www.itying.com/category-93-b0.html


谢谢,看了下原来不是间隔多少秒回调。

能问下ios平台后台定位的实现有哪些方案吗

关于iOS端plus.geolocation.watchPosition的maximumAge参数无效问题,这是由于iOS系统对定位API的实现限制导致的。以下是关键点分析:

  1. iOS系统的CLLocationManager对定位频率有自身控制机制,maximumAge参数在iOS平台可能不会严格遵循设定值

  2. 高精度模式(enableHighAccuracy:true)会触发更频繁的定位回调,这与maximumAge的预期行为存在冲突

  3. 建议的解决方案:

  • 添加防抖逻辑手动控制回调频率
  • 改用定时器+getCurrentPosition组合替代watchPosition
  • 降低定位精度要求(enableHighAccuracy:false)
  1. 代码改进示例:
let lastTime = 0;
const throttleTime = 30000; //30秒间隔

amapLocation = plus.geolocation.watchPosition((position)=>{
    const now = Date.now();
    if(now - lastTime >= throttleTime){
        lastTime = now;
        const {latitude, longitude} = position.coords;
        console.log('定位成功', latitude, longitude, new Date());
    }
}, ...);
回到顶部