当我尝试从 mqtt 通道获取我的纬度和经度值时,我得到 NaN 值?我尝试将它们解析为浮动但仍以 NaN 值结束

I get NaN value when i try to get my latitude and longitude value from a mqtt channel? I try to parse them to float but still end up with a NaN value

我尝试解析我的 GPS 经度和纬度以使其浮动,以便我的 googlemap 可以读取它,但它只能 return NaN。 我通过 mqtt 通道从我的 arduino 发送数据作为浮点数,我的 html 代码正在从中读取数据,有没有人不知道为什么会失败?

window.initialize = initialize;

var redraw = function(payload) {
    lat = parseFloat(payload.message.lat);
    lng = parseFloat(payload.message.lng);

    console.log(lng);

    map.setCenter({ lat: 59.3559, lng:lng, alt: 0 });
    mark.setPosition({ lat: 59.3559, lng:lng, alt: 0 });

    lineCoords.push(new google.maps.LatLng(59.3559,lng));

正如我在评论中所说,消息有效负载是作为字符串接收的(MQTT 消息只是字节数组,由用户将它们转换为 to/from 任何更高级别的格式)。

如果您想将字符串 "{lng: 18.873501}" 用作 JSON 对象 { "lng": 18.873501 } 您需要先解析它。

由于您没有提供 onMessageArrived 函数,因此很难建议如何正确解决此问题,但您可以执行以下操作:

var location = JSON.parse(message.payload);
var lat = location.lat;
var lng = location.lng;