如何更新谷歌地图引擎表

How do I update a Google Map Engine Table?

本文关键字:谷歌地图 引擎 更新 何更新      更新时间:2023-09-26

我在谷歌地图引擎中有一个表格,我想在谷歌网站中使用JavaScript动态更新。 我找到了这个帮助页面,它解释了如何将功能附加到现有表,但我正在努力弄清楚如何修改代码,以便它将更新表而不是附加到它。 我认为我特别需要修改processResponseprocessErrorResponse功能。 但是,我对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"
   }
  }
 ]
}

与其试图将其压缩到对 jpatokal 答案的评论中,不如将其放入答案中。

正如他们所说,您将希望使用batchPatch请求,而不是batchInsert 。 在此处查看文档:https://developers.google.com/maps-engine/documentation/reference/v1/tables/features/batchPatch

如果您使用的是文档中提供的 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));
    }
  });
}

您需要将pathbatchInsert更改为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));
    }
  });
}

batchInsert ,顾名思义,只插入新功能。 请尝试batchPatch