How do I dynamically update a Maps Engine data table with JavaScript? - javascript

I have a Maps Engine map with one of the layers linked to a data table containing a single location. I would like to update the data table via JavaScript in Google Sites. The mapsengine.tables.features.batchPatch seems to do what I want, and the help page for the batchInsert version of the same command seems like it could be modified to do what I want. However, I'm having difficulty getting it to work properly. I believe the problem is due to the fact that I don't know what the primary key is for this table or where I can find it (see here for more explanation).
Can anyone here tell me if I'm headed in the right direction, and how I might be able to find this primary key (it also seems to be referred to as a gx_id at times)? Thanks in advance for whatever help you may be able to provide.
Edit: When I go here and get the information on my table, I get the following response:
{
"tables": [
{
"id": {My Table ID},
"etag": "\"6030101253664097613\"",
"projectId": {My Project ID},
"name": "Current Location",
"description": "",
"tags": [
],
"writersCanEditPermissions": false,
"sourceEncoding": "UTF-8",
"processingStatus": "complete",
"bbox": [
-180,
-90,
180,
90
],
"creationTime": "2014-11-11T21:33:43.982Z",
"lastModifiedTime": "2014-11-12T20:55:20.613Z"
}
]
}
As you can see, there is no gx_id listed. Is there some way to add it, or do I need to recreate the table to be able to add it from the start? If I need to recreate the table, what do I need to do to make sure the gx_id is there, since it wasn't immediately apparent when I created the table last time that anything was missing.

Yes, that should work fine. You can find your gx_id simply by fetching a copy of your table and checking the properties. Here's a sample from the docs:
Request:
https://www.googleapis.com/mapsengine/v1/tables/12421761926155747447-06672618218968397709/features?maxResults=500&version=published&key=(YOUR_KEY_HERE)
Response:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
149.23531999999997,
-35.352484
]
},
"properties": {
...
"gx_id": "1" <-- HERE
}
},

Related

Editing JSON file at multiple positions without rewriting complete file

I have a JSON file that contains lots of nodes. Each node has a unique id. The following is an example skeleton structure of the JSON file.
{
"node1": {
"id": "1",
"name": "student",
"properties": {
"name": "Ram",
"lastname": "Kumar",
"age": "20"
}
},
"node2": {
"id": "2",
"name": "Teacher",
"properties": {
"name": "Ram",
"subject": "Computers"
}
}
Now, this was just sample data. Suppose, I have to find node 2, provided I have the position of the node in the file. That is it's beginning and the end. I create a read stream from the start position and the end position of the file to get the object within the complete JSON object in the file. Now, using JSON.parse function I am able to create an object within the program after parsing it.
But, now I use a writestream to make the changes in the object at the required positions after computing them.
For example, I have to edit the subject property of the teacher, and the main rule is I cannot completely write the object again. Rather, create a writestream at the position where there is subject property and just edit that value.
So, the question is how to do so? And that to with multiple positions to edit, that is editing multiple properties, without rewriting them all.
I know I might face an issue if I use multi-threading for this to simultaneously edit the multiple properties at once, but I need a work around that too.

HTMLized API data

I have a web app that accesses data from an API. I need to pull data out, but it's been 'HTMLized' and I'm unsure how to go about rendering the actual data.
I've tried pulling the data from the array using bracket notation, which gives me the data but it includes the HTML tags.
javascript
{
"id": "5c9cd6576ebf251b944ed16d",
"number": 3451,
"user_id": "5b8425c8e694aa3c6a835b67",
"state": "active",
"subject": "Bower Steel 3D",
"label_ids": [
"5c4b0cf4bbddbd48cd69e68a"
],
"customer_id": "5b51be9ee694aa03f8c834be",
"type": "email",
"reply_to": "xxxxxxx.xxxxxx#xxxxxxx.com",
"reply_cc": "",
"group_id": "5a5f65fed5593070120b9779",
"inbox_id": "5a5f65fed5593070120b9779",
"updated_at": "2019-04-03T12:46:50Z",
"created_at": "2019-03-28T14:12:39Z",
"spam": false,
"trash": false,
"summary": "Great! Glad to hear it.",
"rating": null,
"waiting_since": "2019-04-03T12:21:43Z",
"messages": [
{
"id": "5c9cd6576ebf251b944ed16e",
"type": "reply",
"from_name": "Cxxxxxxx Sxxxxxx",
"body": "<div class=\"5cb74b836ebf2578174d567c\">
<style>cb74b836ebf2578174d567c
p.5cb74b836ebf2578174d567cMsoNormal,
</style>\n\n\n
<div class=\"5cb74b836ebf2578174d567cWordSection1\">\n
<p class=\"5cb74b836ebf2578174d567cMsoNormal\">
Actual content I want to render
</p>
<p class=\"5cb74b836ebf2578174d567cMsoNormal\"></p>\n
<p class=\"5cb74b836ebf2578174d567cMsoNormal\"></p>\n
<p class=\"5cb74b836ebf2578174d567cMsoNormal\"></p>
The content I want to render is embedded like this. It's basically an array within the object, called messages, and it contains a message trail - from the 1st message to the last reply.
I've never come across HTMLized data like this. I can get to the data by using bracket notation, but I don't know how to parse the extraneous tags and get to the data.
Any help would be appreciated.
Short of having the API produce a better endpoint for you to consume, you will have to handle the 'HTMLized' data. In cases like this, I have used something to parse the HTML. For example you could throw the body string into the jQuery constructor.
jQuery(messages[i].body)
That will give you something digestible programmatically to search for your data.

Mapbox GL - how to create polygons for each point on the map

I've been trying to make it work for several days already, and haven't found suitable solution.
I have geojson file with following structure:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ 6.773456,51.227741 ]
},
"properties": {
"City":"Düsseldorf",
"Inhabitants_27_05_1970":671999,
"Inhabitants_25_05_1987":563531,
"Inhabitants_09_05_2011":586291
}
},
...
And for each of this points I need polygon on the map, like this: http://prntscr.com/ie5lh8
Polygon from screenshot is made with the help of tutorial on mapbox website, but I can't find a way to implement it for my data collection, where I only have 2 coordinates and in my geojson file there is no data for this polygons.
Please help.
You seem to be wanting 3D extrusions generated from a lat/lng, not an actual "polygon" on the map.
Take a look at this example:
You can pass in your geojson via variables or a URL, and then assign an arbitrary height that you find works.

Trying to add a hyperlink to Google Sheets via API

I'm using the gapi.client.sheets.spreadsheets.create() method and passing in an object to create a spreadsheet with some predefined values.
I've tried various implementations and haven't yet succeeded in pulling it off. I'm referring to the docs here: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#CellData.
My Object looks something like this:
'sheets': [{
"properties": {
"sheetId": 1,
"title": "Summary",
"index": 0,
},
"data": [
{
"startRow": 0,
"startColumn": 0,
"rowData": [
{
"values": [
{
"hyperlink": "=HYPERLINK('https://google.com')"
}
]
}
}
]
]
Google says: "To set it, use a =HYPERLINK formula". Is this not the hyperlink formula? When the spreadsheet renders the hyperlink field is blank. (I want to display a link to a website). How can this be set?
The documentation for the hyperlink field is "A hyperlink this cell points to, if any. This field is read-only. (To set it, use a =HYPERLINK formula.)". You're still setting the hyperlink field (although you're attempting to set it to a formula). That won't work, because the field is read only. To set a formula, set a value in userEnteredValue.formulaValue. That will set a formula on the server, and the hyperlink field will be populated as a result.
You can also use 'USER_ENTERED' if using batchUpdate:
sheets.spreadsheets.values.batchUpdate({
spreadsheetId,
valueInputOption: 'USER_ENTERED',
requestBody: {
data:[
range: *your range*
values:[['=HYPERLINK("google.com", "ciao")']]
],
},
})
This way you basically put there the formula and the api interprets as if the user entered the formula

What is wrong with my object(regarding knockoutjs mapping plugin)

I'm pulling my hair out with this. I'm not sure what is going on. I've read all the tutorials on mapping but I'm obviously missing something.
{
"address": "110",
"city": "Durham",
"id": 1,
"name": "Keep",
"persistent": true,
"salesRep": "Me",
"state": "NC",
"user": {
"email": "test#test.com",
"id": 4,
"name": "Test",
"password": "test",
"persistent": true
}
}
I've tried
ko.mapping.fromJSON(data);
and
ko.mapping.fromJS(data);
In my old code I ended up doing this.
viewModel.customers(data);
But my JSON looked different. It didn't have a nested object and it also had brackets on the beginning and end making it an array. It seems this would be pretty basic. I'm not getting any errors at all. Thanks for the help.
EDIT
http://jsfiddle.net/gjemN/
In your sample you are dealing with a JavaScript object and not JSON (string representation of it).
So, you would want to call ko.mapping.fromJS. If you are getting back an array of customers, then you could do:
ko.mapping.fromJS(data2, null, viewModel.customers)
Something like: http://jsfiddle.net/rniemeyer/BQe2z/

Categories