# Creating location from lat and lng

**URL:** https://community.tago.io/t/creating-location-from-lat-and-lng/1223
**Category:** Devices and Connectors
**Created:** [August 14, 2022, 6:24pm UTC](https://community.tago.io/t/creating-location-from-lat-and-lng/1223 "2022-08-14T18:24:45Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mello\_roberto](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mello\_roberto](https://community.tago.io/u/mello_roberto)
#### Post date: [August 14, 2022, 6:24pm UTC](https://community.tago.io/t/creating-location-from-lat-and-lng/1223/1 "2022-08-14T18:24:45Z")

</div>

Hello everyone,

I want to build the tag location, to use for GPS.

I have 2 values sent from the device, lat and lng.

How do I combine them to make the location tag?

Thank you

const payload\_raw = payload.find(x =\> x.variable === ‘payload\_raw’ || x.variable === ‘payload’ || x.variable === ‘data’);

if (payload\_raw) {

try {

```
// Convert the data from Hex to Javascript Buffer.

const buffer = Buffer.from(payload\_raw.value, 'hex');

const data = \[

  { variable: 'Timestamp', value: Date(buffer.readUInt32BE(3)).toLocaleString() },

  { variable: 'latitude', value: buffer.readInt32BE(7) / 1000000 },

  { variable: 'longitude', value: buffer.readInt32BE(11) / 1000000 },

  { variable: 'speed', value: buffer.readInt8(15), unit: 'mph' },

  { variable: 'Angle', value: buffer.readInt16BE(16) , unit: 'C' },

  { variable: 'Altitude', value: buffer.readUInt16BE(18) },

  { variable: 'Electricity', value: buffer.readInt8(22), unit: '%' },

  { variable: 'Altitude', value: buffer.readUInt16BE(18) },

\];

// This will concat the content sent by your device with the content generated in this payload parser.

// It also adds the field "serie" and "time" to it, copying from your sensor data.

payload = payload.concat(data.map(x => ({ ...x, serie: payload\_raw.serie, time: payload\_raw.time })));

```

} catch (e) {

```
// Print the error to the Live Inspector.

console.error(e);

// Return the variable parse\_error for debugging.

payload = \[{ variable: 'parse\_error', value: e.message }\];

```

}

console.log(device)

}

---

<div class="post-metadata">

### Author: ![eger](https://yyz2.discourse-cdn.com/flex034/user_avatar/community.tago.io/eger/32/10_2.png) [@eger](https://community.tago.io/u/eger)
#### Post date: [August 16, 2022, 9:48pm UTC](https://community.tago.io/t/creating-location-from-lat-and-lng/1223/2 "2022-08-16T21:48:59Z")

</div>

Hi Roberto,

A location variable must contain an object called ‘location’ with lat and lng inside, e.g:

{

```
variable: 'location',  

value: '26.199906, -80.141655',  

location: {  

    lat: 26.199906,  

    lng: \-80.141655

```

}

}

I updated your parser:

```auto
const payload_raw = payload.find(x => x.variable === 'payload_raw' || x.variable === 'payload' || x.variable === 'data');if (payload_raw) { try { // Convert the data from Hex to Javascript Buffer. const buffer = Buffer.from(payload_raw.value, 'hex');const lng = buffer.readInt32BE(11) / 1000000; const lat = buffer.readInt32BE(7) / 1000000; const data = [{ variable: 'Timestamp', value: Date(buffer.readUInt32BE(3)).toLocaleString() }, { variable: 'location', value: `${lat}, ${lng}`, location: { lat, lng } }, { variable: 'speed', value: buffer.readInt8(15), unit: 'mph' }, { variable: 'Angle', value: buffer.readInt16BE(16) , unit: 'C' }, { variable: 'Altitude', value: buffer.readUInt16BE(18) }, { variable: 'Electricity', value: buffer.readInt8(22), unit: '%' }, { variable: 'Altitude', value: buffer.readUInt16BE(18) },]; // This will concat the content sent by your device with the content generated in this payload parser. // It also adds the field "serie" and "time" to it, copying from your sensor data. payload = payload.concat(data.map(x => ({ ...x, serie: payload_raw.serie, time: payload_raw.time }))); } catch (e) { // Print the error to the Live Inspector. console.error(e); // Return the variable parse_error for debugging. payload = [{ variable: 'parse_error', value: e.message }]; } console.log(device)}

```
