You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Thingsboard谷歌地图获取路线功能异常求助

解决Thingsboard移动端地图路线动作无法获取GPS位置的问题

我在Thingsboard在线演示仪表板搭建GPS系统,设备能正常传输数据,谷歌地图组件也能标记位置,但添加的「获取地图路线」移动端动作完全不工作。使用官方默认代码(如下),即使数据点命名为latitude和longitude也无效:

// Function body that should return location as array of two numbers (latitude, longitude) for further processing by mobile action.
// Usually location can be obtained from entity attributes/telemetry. 

 return getLocationFromEntityAttributes();
// return [30, 30]; // TEST LOCATION


function getLocationFromEntityAttributes() {
    if (entityId) {
        return widgetContext.attributeService.getEntityAttributes(entityId, 'SERVER_SCOPE', ['longitude', 'latitude']).pipe(widgetContext.rxjs.map(function(attributeData) {
            var res = [0,0];
            if (attributeData && attributeData.length === 2) {
                res[0] = attributeData.filter(function (data) { return data.key === 'longitude'})[0].value;
                res[1] = attributeData.filter(function (data) { return data.key === 'latitude'})[0].value;
            }
            return res;
        }));
    } else {
        return [0,0];
    }
}

问题核心

默认代码混淆了异步Observable流和同步返回值:

  • 当entityId存在时,getEntityAttributes返回RxJS Observable,但移动端动作无法处理异步数据流,只能接收直接的数值数组
  • 函数同时存在Observable/数组两种返回类型,导致逻辑冲突

修复方案

替换为以下代码,统一处理异步逻辑并返回符合要求的同步位置数组:

// 使用async/await处理异步属性获取,确保返回同步的位置数组
return await getLocationFromEntityAttributes();

async function getLocationFromEntityAttributes() {
    // 先测试固定坐标,确认移动端动作本身正常
    // return [39.9042, 116.4074];

    if (!entityId) return [0, 0];

    try {
        // 用getEntityAttributesValues直接获取键值对,无需过滤数组
        const attributes = await widgetContext.attributeService.getEntityAttributesValues(
            entityId,
            'SERVER_SCOPE', // 如果GPS数据是遥测,改为'TELEMETRY'
            ['latitude', 'longitude']
        ).toPromise();

        // 返回[纬度, 经度]格式,缺失时用0兜底
        return [
            attributes?.latitude || 0,
            attributes?.longitude || 0
        ];
    } catch (error) {
        console.error('获取位置属性失败:', error);
        return [0, 0];
    }
}

调试步骤

  • 验证动作有效性:先注释异步逻辑,返回固定坐标(比如示例中的北京坐标),确认移动端动作能否正常唤起地图路线功能
  • 确认属性范围:如果GPS数据是设备上传的遥测数据,把代码中的'SERVER_SCOPE'改成'TELEMETRY'
  • 检查键名一致性:确保设备上传的属性键名和代码中的latitude、longitude完全一致(大小写敏感)
  • 查看控制台日志:按F12打开开发者工具→Console标签,查看是否有报错信息定位问题

内容的提问来源于stack exchange,提问作者user23991836

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 10:20:24