google-maps - 谷歌表谷歌地图API,参考错误:谷歌未定义
问题描述
我正在尝试使用以下函数映射谷歌表中两列地址之间的距离。我收到“google not defined”错误,显然是在网页上下文中使用时,尝试在页面完全加载之前访问 google 对象造成的。
为什么我在谷歌表格中收到此错误?我不允许使用相同的 API 吗?
错误是由函数抛出的getDrivingMiles
。
var alert = SpreadsheetApp.getUi().alert
var active_sheet = SpreadsheetApp.getActiveSheet();
var directionsService;
function onOpen()
{
directionsService = new google.maps.DirectionsService();
travel_mode = google.maps.DirectionsTravelMode.DRIVING;
alert = SpreadsheetApp.getUi().alert
active_sheet = SpreadsheetApp.getActiveSheet();
options = [
{
name: "Calculate Mileage",
functionName: "getDistanceInMiles"
}, ];
active_sheet.addMenu("Driving Distance", options);
}
function getColNumberByName(column_name)
{
var whole_sheet_data = active_sheet
.getRange("A1:1")
.getValues();
var col_number = whole_sheet_data[0].indexOf(column_name);
return (col_number)
}
function getColDataByColname(column_name)
{
var col_number = getColNumberByName(column_name);
if (col_number != -1)
{
return active_sheet
.getRange(2, col_number + 1, active_sheet.getMaxRows())
.getValues();
}
}
function getRangeByColname(column_name)
{
var col_number = getColNumberByName(column_name);
if (col_number != -1)
{
return active_sheet.getRange(2, col_number + 1, active_sheet.getMaxRows())
}
}
function getDistanceInMiles()
{
var origins = getColDataByColname('From Address');
var destinations = getColDataByColname('To Address');
var distance_col = getColNumberByName('Distance in Miles');
var num_addresses = origins.length;
var origin = "";
var destination = "";
var distance = 0;
for (var i = 0; i < num_addresses; i++)
{
origin = origins[i][0];
destination = destinations[i][0];
if (origins[i][0] == "")
{
break;
}
Logger.log(origin + " to " + destination);
var request = {
origin: origin,
destination: destination,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status)
{
if (status == google.maps.DirectionsStatus.OK)
{
Logger.log(response.routes[0].legs[0].distance.value); // the distance in metres
}
else
{
// oops, there's no route between these two locations
// every time this happens, a kitten dies
// so please, ensure your address is formatted properly
}
});
}
}
function getDrivingMiles(origin, destination)
{
Utilities.sleep(1000)
var directions = Maps.newDirectionFinder()
.setOrigin(origin)
.setDestination(destination)
.getDirections();
if (directions && directions.error_message)
{
throw directions.error_message
}
if (directions && directions.routes && directions.routes[0] && directions.routes[0].legs && directions.routes[0].legs[0] && directions.routes[0].legs[0].distance)
{
return directions
.routes[0]
.legs[0]
.distance
.value/1609.34;
}
return "";
}
解决方案
尽管 Google Apps 脚本是 Javascript 的子集并且共享许多功能,但它们的编程方式并不相同。也就是说,由于在您的应用程序(或默认情况下的任何 GAS 应用程序)的全局范围内没有声明“google”对象,因此在引用它时会返回错误。
为了从您的 GAS 应用程序访问 Maps API,您应该使用或者URLFetchApp
甚至更好地使用受支持的内置服务。在您的情况下,这将使用Maps Service。
您可以在以下链接中查看更多信息:
推荐阅读
- html - 自动翻译网站
- amazon-web-services - 适用于 Amazon Cogntio 的基于 AWS IAM 标签的策略
- sql - Oracle SQL - 来自 Group by 的最大限制
- mysql - MySQL - 如何在 JSON 中查找出现次数?
- javascript - 如何在不从 reactjs 中的 firebase 数据库重新加载页面的情况下获取数据
- blazor - 如何查看组件的层次结构
- monit - 使用监控工具监控多台服务器
- python - sigma 的 pi 分布的奇怪增加增加了重复 Python 的时间
- javascript - 如何将 Promise.all 与多个 Firestore 查询一起使用
- node.js - 带有 Typeorm 的 NestJS 无法解决依赖关系