谷歌地图 javascript api 无需浏览器

aug*_*chy 5 google-maps google-maps-api-3 node.js

我正在尝试从谷歌地图获取特定信息,该信息只能通过 JavaScript API 获得。编辑我特别对响应的“步骤”、“腿”和“overview_path”部分感兴趣。这些数据只能使用 JavaScript API 接收;参见 下面的(直接方向)API 调用的响应,以查看是否存在此信息;https://maps.googleapis.com/maps/api/distancematrix/json?origins=46.210553,6.1430196&destinations=47.54758899999999,7.589662&mode=transit&transit_mode=rail&key=YOUR_API_KEY

\n

但是,我不需要任何类型的可视化或地图,只需绘制对我的请求的 json 响应。

\n

所以我想在我的终端中通过 NodeJS 运行请求,下面的代码应该将请求和响应输出到我的终端。

\n

我\xe2\x80\x99ve 遇到了googlemaps/js-api-loader可用于本地测试的库,我想我\xe2\x80\x99m 在这里所做的(?):

\n

编辑我在最初的问题中错误地使用了 DistanceMatrix 而不是 DirectionsService 。正如@MrUpsidown 在评论中指出的那样,我可以使用 google-maps-services-js NodeJS 库。我更新了代码,您可能会再次争论我可以使用这个库,但是,如上所述,我想要的信息并未包含在 API 的响应中。

\n
const { Loader } = require("@googlemaps/js-api-loader");\n\nconst loader = new Loader({\n  apiKey: API_KEY,\n  version: "weekly"\n});\n\nloader.load().then(() => {\n  // initialize services\n  const directionsService = new google.maps.DirectionsService()\n\n  directionsService\n  .route({\n    origin: "Genf",\n    destination: "Basel",\n    travelMode: google.maps.TravelMode.TRANSIT,\n  })\n  .then((response) => {\n    console.log(JSON.stringify(response, null, 2));\n  })\n});\n
Run Code Online (Sandbox Code Playgroud)\n

这就是我陷入困境的地方,因为在终端中运行它会引发一个错误,到目前为止我无法修复该错误。

\n
~$ node index.js\nReferenceError: window is not defined.\n
Run Code Online (Sandbox Code Playgroud)\n

我\xe2\x80\x99已经读到可能可以模拟窗口或dom对象,但我\xe2\x80\x99m对javascript和webdev相当陌生,所以我\xe2\x80\x99d很高兴在这里得到一些帮助。

\n

到目前为止,我想出的最好的解决方案是一种肮脏的解决方法:\xe2\x80\xa8有一个谷歌示例,它可以启动一个本地托管的网站,可以通过运行调用 localhost 在浏览器中打开该网站

\n
~$ npm i\n~$ npm start\n
Run Code Online (Sandbox Code Playgroud)\n

通过修改此示例中的请求,我\xe2\x80\x99m 能够获取我需要的信息。我使用 pyppeteer 作为无头浏览器来自动化此操作,以检索响应以进行进一步处理。

\n

我的希望是,它可以在没有 pyppeteer / puppeteer 的情况下完成,因为由于我缺乏理解,这似乎有点矫枉过正(和过度工程)。

\n

预先感谢您的任何提示和提示!

\n

Yrl*_*rll 3

地图 Javascript API 是一项CLIENT-SIDE服务

使用节点在服务器端运行 Maps Javascript 时遇到困难的原因是它不是为了以这种方式使用的。尽管Maps Javascript API包含Directions Service,但这并不意味着您可以在服务器端使用它。

请注意,它被称为client-side服务,因为它应该与数据可视化一起使用。在本例中,在浏览器中加载地图。

那么,如果您想在 上运行 Directions API,那么您应该使用什么呢server-side

使用独立的路线 API Web 服务

文档具体说明了这一点:

Directions API 是一种 Web 服务,它使用 HTTP 请求返回位置之间的 JSON 或 XML 格式的方向。路线有多种形式:

要使用节点执行此操作,您可以axios通过在 JavaScript 代码中要求它来使用它。Directions API Web 服务文档中有一个代码示例,但我为您修改了一个代码示例以满足您的需求。特别是在获取steps,legsoverview_polyline.

以下是使用您针对问题提供的示例请求的代码示例:

var axios = require('axios');

var config = {
  method: 'get',
  url: 'https://maps.googleapis.com/maps/api/directions/json?origin=Geneva&destination=Basel&mode=%20transit&key=API_KEY',
  headers: { }
};

axios(config)
.then(function (response) {
  // This is the JSON object result for the directions request
  const result = response.data;
  // Logging the `legs` array
  console.log(result.routes[0].legs);
  // Logging the `steps` array
  console.log(result.routes[0].legs[0].steps);
  // Logging the `overview_polyline`
  console.log(result.routes[0].overview_polyline)
})
.catch(function (error) {
  console.log(error);
}); 
Run Code Online (Sandbox Code Playgroud)

设置节点环境后,您只需在终端上运行此代码:

node index.js
Run Code Online (Sandbox Code Playgroud)

这将在控制台上记录您想要的结果。

ps,您还可以使用 MrUpsidown 在您的问题评论中提供的 NodeJs 客户端库:https://github.com/googlemaps/google-maps-services-js

希望这可以帮助!