I have a table in Google Maps Engine that I would like to update dynamically with JavaScript in a Google Site. I've found this help page that explains how to append features to an existing table, but I'm struggling to figure out how to modify the code so that it will update the table instead of append to it. I believe I specifically need to modify the processResponse
and processErrorResponse
functions. However, I'm fairly new to JavaScript/jQuery/JSON, and I'm not exactly sure how to determine what I should have instead of #insert-table-features-response
. Is there someone here that could explain that to me?
Edit: To put it another way, how can I make the request shown below with JavaScript?
POST https://www.googleapis.com/mapsengine/v1/tables/{YOUR_TABLE_KEY}/features/batchPatch?key={YOUR_API_KEY}
Content-Type: application/json
Authorization: Bearer {. . .}
X-JavaScript-User-Agent: Google APIs Explorer
{
"features": [
{
"geometry": {
"type": "Point",
"coordinates": [
-82,
35
]
},
"properties": {
"Lat": 35,
"Long": -82,
"Name": "6:41:13 AM 11/27/14",
"gx_id": "123ABC456DEF7890"
}
}
]
}
Rather than try and squeeze this into a comment on jpatokal's answer, I'll throw it in an answer.
As they said, you will want to use the batchPatch
request, rather than batchInsert
. Check out the docs here: https://developers.google.com/maps-engine/documentation/reference/v1/tables/features/batchPatch
If you're using the JS provided in the docs, then the code on page you linked to includes this function:
function insertTableFeatures(tableId) {
doRequest({
path: '/mapsengine/v1/tables/' + tableId + '/features/batchInsert',
method: 'POST',
body: {
features: cities
},
processResponse: function(response) {
$('#insert-table-features-response').text(
JSON.stringify(response, null, 2));
},
processErrorResponse: function(response) {
$('#insert-table-features-response').text('Error response:\n\n' +
JSON.stringify(response, null, 2));
}
});
}
You will want to change the path
from batchInsert
to batchPatch
and update the body: { ... }
. You can replace it with the body of the HTTP request you provided like so:
function updateTableFeatures(tableId) {
doRequest({
path: '/mapsengine/v1/tables/' + tableId + '/features/batchPatch',
method: 'POST',
body: {
"features": [
{
"geometry": {
"type": "Point",
"coordinates": [
-82,
35
]
},
"properties": {
"Lat": 35,
"Long": -82,
"Name": "6:41:13 AM 11/27/14",
"gx_id": "123ABC456DEF7890"
}
}
]
},
processResponse: function(response) {
// You'll probably want to change these too
$('#insert-table-features-response').text(
JSON.stringify(response, null, 2));
},
processErrorResponse: function(response) {
// You'll probably want to change these too
$('#insert-table-features-response').text('Error response:\n\n' +
JSON.stringify(response, null, 2));
}
});
}