google-maps - Google API 的 ImportXML / HTML 问题
问题描述
我正在努力从 importxml 到 googlesheet 获得任何结果。我以前得到的是反映距离的输出,但现在我得到一个错误“导入的内容是空的”。
=importxml("http://maps.googleapis.com/maps/api/directions/xml?origin=" & B9 & "&destination=" & D9 & "&sensor=false&alternatives=false","//leg/distance/value")/1000*0.621371
有谁知道这里的解决方法?我也尝试过使用我的 api 密钥并遇到了类似的问题:
=IMPORTHTML("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=bay+shore,ny&destinations=New+York+City,NY&key=XXXXXXX", "table",3)
非常感谢任何帮助。
解决方案
这只是一个初学者的尝试,因此欢迎改进,但是查看 geocodezip 发布的 URL,您可以将该 URL 传递给UrlFetchApp.fetch
并JSON.parse
在getContentText()
响应中使用。
您可以在工作表中用作用户定义的函数并通过控制台检查Logger.log(GETDISTANCE());
function GETDISTANCE() {
var res= UrlFetchApp.fetch("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=bay+shore,ny&destinations=New+York+City,NY&key=");
var content = res.getContentText();
var json = JSON.parse(content);
return json.rows[0]["elements"][0]["distance"].text;
}
您希望将 URL 和位置部分移动到传递给函数的参数中,以使其更加通用。
我通过检查 JSON 结构确定了用于提取里程文本的路径,如下所示:
推荐阅读
- javascript - 单击和确认时更改按钮的颜色
- python - 在 Django 查询集中动态定义过滤器参数
- typescript - react-scripts 从 3.4 更新到 4.0 导致 Jest 版本错误
- cytoscape.js - 在 Windows 10 中安装 Cytoscape 3.8.2 时出错
- c++ - C ++创建具有非常量大小的二维数组,
- flutter - 在 Flutter 中监听 Stream 并只向 DB 写入一次
- javascript - 我的 jquery 代码不适用于使用 ajax 更改页面
- python - Bottle and Mysql blocking request
- python - 查找所有依赖于 linux 系统库的 Python 包
- c++ - std::string 和向量之间的区别
在 C++ 中