cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

How to solve the NaN value in updated location field

tguo
4-Participant

How to solve the NaN value in updated location field

My code is to get a new field of location from available column longitude, latitude, elevation. Built with no error.

// result: INFOTABLE  dataShapeName : "MapData"
var result = Things["ThingName"].ServiceName(); // Change the ThingName and Service name here

var newField = new Object();
newField.name = "location";
newField.baseType = 'LOCATION';
result.AddField(newField);

// location:LOCATION
var temp = new Object();

var tableLength = result.rows.length;
for (var x = 0; x < tableLength; x++) {
    var row = result.rows
    temp.latitude = row.latitude;//result.rows.latitude;
    temp.longitude = row.longitude;//result.rows.longitude;
    temp.elevation = 0;
    temp.units = "WGS84";
    row.location = temp;
}

But the output is like this. The location column is all NaN:NaN. Something is wrong?

WeChat Image_20170519094705.png

ACCEPTED SOLUTION

Accepted Solutions
qngo
12-Amethyst
(To:qngo)

The "result" in my test is a new InfoTable of DataShape NamedVTQ, created directly in service. Then I added 10 rows with only "value" before adding the new field "location". The "latitude" and "longitude" are just 0,1,2...

var data = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({dataShapeName: "NamedVTQ"});

for (var i = 0; i < 10; i++) {

    data.AddRow({name: "name_" + i})   ;

}

var newField = new Object(); 

newField.name = "mylocation"; 

newField.baseType = 'LOCATION'; 

data.AddField(newField);

var tableLength = data.length; 

for (var x = 0; x < 10; x++) { 

    var row = data;

    var tmp = new Object();

  

    tmp.latitude = x;

    tmp.longitude = x * 10;/

    tmp.elevation = 0; 

    tmp.units = "WGS84"; 

  

// 3 rows below work

//    data.mylocation = tmp;

//    data.rows.mylocation = tmp;

    row.mylocation = tmp;

  

    data.AddRow({mylocation: tmp, value: tmp.latitude});

  

    data.value = tmp.latitude;

}

data.AddRow({mylocation: tmp, name: "name_99", value: 99});

logger.debug(data.rows);

result = data;

View solution in original post

13 REPLIES 13
yshen-2
5-Regular Member
(To:tguo)

Pleas move

var temp = new Object();

inside for loop

other wise you are setting all the location field to one object and do the change on one object


Thanks

YUanwu Shen

tguo
4-Participant
(To:yshen-2)

My code is like this now:

var tableLength = result.rows.length;

for (var x = 0; x < tableLength; x++) {

    var row = result.rows;

    var temp = new Object();

temp.baseType = 'LOCATION';

    temp.latitude = row.latitude;

    temp.longitude = row.longitude;

    temp.elevation = 0;

    temp.units = "WGS84";

    result.rows.location = temp;

}

My output still the same...

WeChat Image_20170519175905.png

tguo
4-Participant
(To:yshen-2)

Dear Yuan Wu,

      Thanks for your help. Please see my latest update. There is new problem in the google map... Maybe the precision of the digits in the location?

ankigupta
12-Amethyst
(To:tguo)

Hi Tiantian Guo​,

row.location = temp;  is incorrect

it should be result.rows.location = temp;

tguo
4-Participant
(To:ankigupta)

In the loop script,

var row = result.rows;

So I think the code is right.

qngo
12-Amethyst
(To:tguo)

Hello,

The behavior is weird. Before the end of for loop, if I add "result.AddRow({location: temp}), the new rows are added with correct location. When I log the result, the data structure is different too.

"location" where is Nan: "location={"elevation":1.0,"latitude":1.0,"units":"WGS84","longitude":0.0}"

Correct "location" added at the end of result: "location=1.0,1.0,0.0"

ankigupta
12-Amethyst
(To:qngo)

That's Strange because it works fine for me.

The only difference is I am not using the DataBase. My data is stored in a DataTable.

qngo
12-Amethyst
(To:qngo)

The "result" in my test is a new InfoTable of DataShape NamedVTQ, created directly in service. Then I added 10 rows with only "value" before adding the new field "location". The "latitude" and "longitude" are just 0,1,2...

var data = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({dataShapeName: "NamedVTQ"});

for (var i = 0; i < 10; i++) {

    data.AddRow({name: "name_" + i})   ;

}

var newField = new Object(); 

newField.name = "mylocation"; 

newField.baseType = 'LOCATION'; 

data.AddField(newField);

var tableLength = data.length; 

for (var x = 0; x < 10; x++) { 

    var row = data;

    var tmp = new Object();

  

    tmp.latitude = x;

    tmp.longitude = x * 10;/

    tmp.elevation = 0; 

    tmp.units = "WGS84"; 

  

// 3 rows below work

//    data.mylocation = tmp;

//    data.rows.mylocation = tmp;

    row.mylocation = tmp;

  

    data.AddRow({mylocation: tmp, value: tmp.latitude});

  

    data.value = tmp.latitude;

}

data.AddRow({mylocation: tmp, name: "name_99", value: 99});

logger.debug(data.rows);

result = data;

tguo
4-Participant
(To:qngo)

Yes. If I add the a new row,

var tableLength = result.rows.length;
for (var x = 0; x < tableLength; x++) {
    var row = result.rows;
    var temp = new Object();
temp.baseType = 'LOCATION';

    temp.latitude = row.latitude;
    temp.longitude = row.longitude;
    temp.elevation = 0;
    temp.units = "WGS84";
    result.rows.location = temp;

    result.AddRow({location: temp});

}

The result is right then:

WeChat Image_20170519180239.png

ankigupta
12-Amethyst
(To:tguo)

Hi Tiantian Guo​,

Based on Quang input; I have following suggestion:

1. Create a new DataShape which has all the fields you require + location field. DataShape1

2. In custom Service create a new Infotable using above DataShape.

var params = {

    infoTableName: "INFOTABLE" /* STRING */,

    dataShapeName: "DataShape1" /* DATASHAPENAME */

};

// result: INFOTABLE

var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

3. Loop though the old infotable (let's say result1) and Add the Row to the new infotable (result).

var tableLength = result1.rows.length;

for (var x = 0; x < tableLength; x++) {

     var temp = new Object();

     temp.elevation = 0;

     temp.units = "WGS84";

     temp.latitude = result1.rows.latitude;

     temp.longitude = result1.rows.longitude;

     var temp1 = new Object();

     temp1.latitude = result1.rows.latitude;

     temp1.longitude = result1.rows.longitude;

     temp1.location = temp;

     result.AddRow(temp1);

}

I hope it helps.

tguo
4-Participant
(To:ankigupta)

Dear Ankit,

     I got the right result using your code.

WeChat Image_20170520095115.png

And I attach the result to the google map,

WeChat Image_20170520095209.png

There are nothing appearing.

WeChat Image_20170520095238.png

I thought maybe the precision of the location, there are lacking two digits from the original latitude and longitude..

ankigupta
12-Amethyst
(To:tguo)

Hi Tiantian Guo​,

Could you please start a new Thread for this issue and add a Screenshot of Google widget properties.

Have you selected the location column for location property in Google widget?

tguo
4-Participant
(To:ankigupta)

Dear Ankit,

         I have selected the location column for location property.

         WeChat Image_20170522113210.png

Announcements


Top Tags