Fix worldmap marker location from the field device realtime data

Hello,

I am new using Node-RED, I have a problem with fixing marker in Worldmap node with using a realtime data. I can solve it if I directly write the number of lat and lon and feed them to worldmap-UI through a function with if-statement if msg.payload.action = true.

I tried to store a live data of latitude and longitude to a new variable. I thought it will work since

const lati = msg.payload.latitude //lat value from the device
const longi = msg.payload.longitude //lon value from the device

if (msg.payload.action = true)
{
msg.payload = {
"lat": lati,
"lon": longi,
"name" : "myMarker"
}
}
return msg;

What i realized is the msg.payload.lat and msg.payload.lon values will become undefined if there is a change of event (for example if I go to another tab in the UI). Therefore the marker will be gone. it will be back if I receive a realtime data from the device.

Is there any solution to fix the marker from a realtime data? Thank you!

I have read this similar problem

map_disconnect_event_Marker|690x308

The map itself does not store data - so yes you need to save any points you need in the future in context - Working with context : Node-RED - and then resend the data when the worldmap connects to the server using the worldmap in node se tot detect connect events and use that to trigger the resend of data.

Hi, Thanks for answering. Could you please kindly give an example? I have tried to save msg.payload to flow.myData or to global.mydata using change node. Then I connect the worldmap in to function node and then feed them to the worldmap. It still didn't work instead there is and error that one of the declared property is not known on undefined.

Here I have one marker in the inject node. It contains "name", "latitude" and "longitude" property. I feed them to the change node so that the msg.payload will be stored in either flow.myData (or global.myData) and then again in the function node I wrote as follow:

const lati = global.myData.latitude
const longi = global.myData.longitude
if (msg.payload.action = true)
{
msg.payload = {
"lat": lati,
"lon": longi,
"name" : "myMarker"
}
}

return msg;

I connect worldmap in to the function node and connect function node to the worldmap-UI. Also, I tried to get the output of flow.myData or global.myData though a debug node with an expression (flow.myData) with no luck.

Here is a simple example

[{"id":"4c0d05bd.3b452c","type":"worldmap in","z":"847a21f.bae23e","name":"New Connection","path":"/worldmap","events":"connect","x":300,"y":800,"wires":[["3059d27e.22263e"]]},{"id":"599aad2c.d08ab4","type":"worldmap","z":"847a21f.bae23e","name":"","lat":"","lon":"","zoom":"","layer":"","cluster":"","maxage":"1000","usermenu":"show","layers":"show","panit":"true","panlock":"false","zoomlock":"false","hiderightclick":"false","coords":"none","showgrid":"false","path":"/worldmap","x":700,"y":860,"wires":[]},{"id":"6c2212f0.be29ac","type":"inject","z":"847a21f.bae23e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":860,"wires":[["49688673.4a7bd8"]]},{"id":"3059d27e.22263e","type":"function","z":"847a21f.bae23e","name":"","func":"var points = context.get(\"points\") || {};\n\nif (msg.topic === \"worldmap\") {\n    return {payload:Object.values(points)};\n}\nelse {\n    points[msg.payload.name] = msg.payload;\n}\ncontext.set(\"points\",points);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":860,"wires":[["599aad2c.d08ab4","8f2debc0.65abb8"]]},{"id":"49688673.4a7bd8","type":"function","z":"847a21f.bae23e","name":"add point","func":"msg.payload = {\n    name:\"Thing\",\n    lat:51.05,\n    lon:-1.35,\n    icon:\"mayflower\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":860,"wires":[["3059d27e.22263e"]]},{"id":"8f2debc0.65abb8","type":"debug","z":"847a21f.bae23e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":690,"y":920,"wires":[]}]

Hi, it works! Thank you very much.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.