我在Google Maps Engine中有一个表格,我想在Google网站上使用JavaScript动态更新。我找到了this帮助页面,解释了如何将功能附加到现有表中,但我正在努力弄清楚如何修改代码以便它更新表而不是附加到它。我相信我特别需要修改processResponse
和processErrorResponse
函数。但是,我对JavaScript / jQuery / JSON还不熟悉,而且我不确定如何确定我应该拥有的内容而不是#insert-table-features-response
。这里有人可以向我解释一下吗?
修改:换句话说,如何使用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"
}
}
]
}
答案 0 :(得分:2)
我不会试着把它压成对jpatokal答案的评论,而是把它扔进答案。
正如他们所说,您需要使用batchPatch
请求,而不是batchInsert
。在这里查看文档:{{3}}
如果您使用的是文档中提供的JS,那么您链接到的页面上的代码包含此功能:
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));
}
});
}
您需要将path
从batchInsert
更改为batchPatch
并更新body: { ... }
。您可以将其替换为您提供的HTTP请求的正文,如下所示:
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));
}
});
}
答案 1 :(得分:1)
batchInsert
,名称正确,仅插入新功能。请改为batchPatch
。