Skip to main content
1-Visitor
May 18, 2017
Solved

How to import google map location data from database

  • May 18, 2017
  • 3 replies
  • 8545 views

Hi all,

    I have a problem on how to import google map location data from database.

    The longitude, latitude, elevation are stored in different column in the postgresql database. I want to connect the database to the google map widget.

    How can I do this?

    BTW:

    I know how to insert location data through Thing->Services (javascript) and extract location data also through Thing->Services to a datashape and bind the datashape to the google map. But now the location data will from columns of database? I am a little confusing.

Best answer by ankigupta

Hi Tiantian Guo​,

Please try something like:

// result: INFOTABLE  dataShapeName : "MapData"

var result = Things["DBThing"].Service1(); // Change the ThingName and Service name here

var newField = new Object();

newField.name = "buildinglocation";

newField.baseType = 'LOCATION';

result.AddField(newField);

// location:LOCATION

var location = new Object();

var tableLength = result.rows.length;

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

var row = result.rows;

    location.latitude = row.latitude;

    location.longitude = row.longitude;

    location.elevation = 0;

    location.units = "WGS84";

    result.rows.buildinglocation = location;

}

I hope it helps.

3 replies

5-Regular Member
May 18, 2017

Hi Tiantian Guo​,

Per my understanding; this will require conversion of longitude, latitude, elevation data to a Location Object.

Steps:

1. Read longitude, latitude, elevation Data from DataBase in an infotable.

2. Loop through the Table and convert longitude, latitude, elevation data to Location Object infotable. Example:

// location:LOCATION

var location = new Object();

location.latitude = latitudeFromDataBase;

location.longitude = longitudeFromDataBase;

location.elevation = elevationFromDataBase;

location.units = "WGS84";

3. Bind the new infotable to the Google widget.

I hope it helps.

tguo1-VisitorAuthor
1-Visitor
May 18, 2017

Thanks Ankit. I am now doing like this.

But I now want to get a new column "location" from other three columns. I found some solutions. The grammar for thingworx needs to get used to..

Currently my code is like:

var params = {
infoTableName : "MapDataWithLocation",
dataShapeName : "MapData"
};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(MapData)
var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var buildinglocation = new Object();
buildinglocation.name = "buildinglocation";
buildinglocation.baseType = 'location';

result

got some errors.. still working..

tguo1-VisitorAuthor
1-Visitor
May 18, 2017

Finally the code is no bug:

var params = {
infoTableName : "MapDataWithLocation",
dataShapeName : "MapData"
};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(MapData)
var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

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

var tableLength = result.rows.length;

for (var x = 0; x < tableLength; x++) {
var row = yourInfotableHere.rows;
//Your code here
    row.buildinglocation.latitude = row.latitude;
    row.buildinglocation.longitude = row.longitude;
    row.buildinglocation.elevation = 0;
    row.buildinglocation.units ="WGS84";
}

My input is a datashape.

The datashape is a output from the database.

But this code output is just fields of a new datashape. Where I get the data?... Really a newbie, sorry and thanks!

ankigupta5-Regular MemberAnswer
5-Regular Member
May 18, 2017

Hi Tiantian Guo​,

Please try something like:

// result: INFOTABLE  dataShapeName : "MapData"

var result = Things["DBThing"].Service1(); // Change the ThingName and Service name here

var newField = new Object();

newField.name = "buildinglocation";

newField.baseType = 'LOCATION';

result.AddField(newField);

// location:LOCATION

var location = new Object();

var tableLength = result.rows.length;

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

var row = result.rows;

    location.latitude = row.latitude;

    location.longitude = row.longitude;

    location.elevation = 0;

    location.units = "WGS84";

    result.rows.buildinglocation = location;

}

I hope it helps.

tguo1-VisitorAuthor
1-Visitor
May 18, 2017

Dear Ankit,

      It is really helpful. I think I start to understand the thingworx function now..

      I change your code like this:

// result: INFOTABLE  dataShapeName : "MapData"
var result = Things["ExtractData"].extractdata(); // 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();
temp.baseType = 'LOCATION';

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

But the output like this:

WeChat Image_20170518172851.png

something wrong here. But it really a big step for me. Thanks!

5-Regular Member
May 19, 2017

Ankit Gupta​ I see this thread has already been answered and Tiantian Guo​ has opened another for issue on NaN could you please have following discussion on that thread i.e. How to solve the NaN value in updated location field

Thanks

Sushant

5-Regular Member
May 19, 2017

Sushant Pandey​ Sure!