# Converting MQTT location to GEOFENCE GPS

**URL:** <https://discourse.nodered.org/t/converting-mqtt-location-to-geofence-gps/5629>\
**Category:** General\
**Created:** [10 December 2018 01:52 UTC](https://discourse.nodered.org/t/converting-mqtt-location-to-geofence-gps/5629 "2018-12-10T01:52:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hvddrift](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hvddrift/32/804_2.png) [@hvddrift](https://discourse.nodered.org/u/hvddrift)\
**Post date:** [10 December 2018 01:52 UTC](https://discourse.nodered.org/t/converting-mqtt-location-to-geofence-gps/5629/1 "2018-12-10T01:52:35Z")

</div>

Hi

My understanding of Java is quite low, however, I would like some help writing a function.

My mqtt device tracker gives this payload:

12/10/2018, 11:47:33 AM[node: 978c90cb.73b7]  
zanzito/shared\_locations/my\_cell : msg.payload : string[148]  
{"battery\_level":83,"longitude":143.5514104,"latitude":252122.447,"altitude":47.70000076293945,"gps\_accuracy":22,"user":"my\_cell","tst":1544406310}

I would like to extract the longitude and latitude and rename it to one of these 3 formats

msg.location.lat & msg.location.lon  
msg.lat & msg.lon  
msg.payload.lat & msg.payload.lon

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 December 2018 02:49 UTC](https://discourse.nodered.org/t/converting-mqtt-location-to-geofence-gps/5629/2 "2018-12-10T02:49:35Z")

</div>

So what have you tried so far?

---

<div class="post-metadata">

**Author:** ![hvddrift](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hvddrift/32/804_2.png) [@hvddrift](https://discourse.nodered.org/u/hvddrift)\
**Post date:** [10 December 2018 02:58 UTC](https://discourse.nodered.org/t/converting-mqtt-location-to-geofence-gps/5629/3 "2018-12-10T02:58:43Z")

</div>

I may have just worked it out, not by using a function, however.

This was my function.

> msg = {  
> location:{  
> lat:msg.data.latitude,  
> lon:msg.data.longitude  
> },  
> }  
> return msg;

That did not work.

I then used the following nodes:

- JSON node to convert the string
- Change node with the following rules
  - Set location.lat -\> payload.latitude
  - Set location.lon -\> payload.longitude

I am sure this could be written as a function, but it works.

The function now will be a mental exercise.
