Google maps 在脚本编辑器中设置映射API键
据我所知,为了跟踪我们的配额使用情况,我们需要在我们计划使用的服务上提供谷歌应用程序服务的API密钥 在我的例子中,我有一个带有起点和终点的电子表格和一个自定义函数来计算两者之间的距离 调用Google maps 在脚本编辑器中设置映射API键,google-maps,google-apps-script,custom-function,Google Maps,Google Apps Script,Custom Function,据我所知,为了跟踪我们的配额使用情况,我们需要在我们计划使用的服务上提供谷歌应用程序服务的API密钥 在我的例子中,我有一个带有起点和终点的电子表格和一个自定义函数来计算两者之间的距离 调用.getDirections(),我遇到了满足配额的问题: 错误:服务在一天内调用次数过多:路由。(第**行) 代码示例: function getDirections_(origin, destination) { var directionFinder = Maps.newDirectionFind
.getDirections()
,我遇到了满足配额的问题:
错误:服务在一天内调用次数过多:路由。(第**行)
代码示例:
function getDirections_(origin, destination) {
var directionFinder = Maps.newDirectionFinder();
directionFinder.setOrigin(origin);
directionFinder.setDestination(destination);
var directions = directionFinder.getDirections();
return directions;
}
所以我读到,如果我将API密钥分配给我的项目,我应该能够看到使用情况以及我离免费配额有多近
在脚本编辑器中,我启用了Resources菜单/AdvancedGoogleServices下的所有API。然后我去了谷歌开发者控制台,在那里
我没有看到任何关于我的自定义函数调用GoogleMapsAPI或任何API使用次数的记录
从逻辑上讲,我认为在我的脚本中,我需要设置我的GoogleAPI密钥,以便我的脚本开始以我的用户名调用API,并计算我使用某些API的次数。我想现在我使用的谷歌地图API是匿名的,因为整个公司都被分配了相同的IP,所以我们用尽了允许的号码来调用这个函数
底线请回答,如果你知道一种方法来连接我的简单电子表格功能的公共API访问密钥我有
谢谢,,
保罗我也渴望找到这个答案很久了,我很高兴地说我找到了。根据更新日期,谷歌可能在2015年10月14日左右发布了此版本 您可以利用UrlFetchApp添加API密钥。我上面发布的链接应该有助于获取该密钥
function directionsAPI(origin, destination) {
var Your_API_KEY = "Put Your API Key Here";
var serviceUrl = "https://maps.googleapis.com/maps/api/directions/json?origin="+origin+"&destination="+destination+
"&mode="+Maps.DirectionFinder.Mode.DRIVING+"&alternatives="+Boolean(1)+"&key="+Your_API_KEY;
var options={
muteHttpExceptions:true,
contentType: "application/json",
};
var response=UrlFetchApp.fetch(serviceUrl, options);
if(response.getResponseCode() == 200) {
var directions = JSON.parse(response.getContentText());
if (directions !== null){
return directions;
}
}
return false;
}
所以在代码中穿行。。。首先输入API密钥。然后在var serviceUrl中选择您的参数。我加入了其他参数(模式和备选方案),以展示如何添加它们。如果您不想要它们,请将其移除
使用UrlFetch,您可以添加选项。我使用了muteHttpExceptions,以便在响应代码指示失败时提取不会引发异常。这样我们就可以为函数选择返回类型,而不是抛出异常。我对内容类型使用JSON,因此我们可以使用相同的格式发送和检索请求。响应代码为200表示成功,因此directions随后将解析并像getDirections()将返回的对象一样执行操作。如果UrlFetch未成功(另一个响应代码)或对象为null,则函数将返回false
当您查看Google Maps Directions API时,您将能够在开发人员控制台中实时查看查询。请确保启用了计费功能,一旦超过配额,您将被收取费用。从2017年7月13日起,我能够通过在“高级谷歌服务”菜单(图片1和2)和谷歌开发者控制台中启用Sheets API来使API正常工作。如果您使用相同的电子邮件地址登录Google Sheets,则不需要获取功能 [在资源菜单中,选择高级谷歌服务。][1] [在高级Google服务中,确保Google Sheets API已打开。][2] [1] : [2] :1。)我从控制台仪表板添加了一个API密钥。请记住选择您正在处理的正确项目 2.)在我的项目(脚本编辑器)中,我使用API密钥和控制台中的客户端ID将身份验证设置为映射。我已将脚本包括在下面:
function getDrivingDirections(startLoc, wayPoint, endLoc){
var key = "Your_API_Key";
var clientID = "Your_Client_ID";
Maps.setAuthentication(clientID, key);
var directions = Maps.newDirectionFinder()
.setOrigin(startLoc)
.addWaypoint(wayPoint)
.setDestination(endLoc)
.setMode(Maps.DirectionFinder.Mode.DRIVING)
.getDirections();
} return directions;
谢天谢地,卡林先生!谢谢你以上的回答。JP的回答也解释了他的代码。只是为了分享,没有代码解释(看看上面的JP Carlin的解释),下面是我的版本。您将看到,我还具有出发时间参数,以便获得特定日期时间的距离和驾驶分钟数。我还添加了一个日志错误调用(在“查看/日志”下查看): 注意:谷歌支持人员告诉我,不支持将谷歌地图的API密钥(例如带有“方向API”)与谷歌表单一起使用。下面的代码可以工作,但是不受支持的变通方法。截至2018年11月4日,谷歌有一项内部申请,要求在谷歌表单中添加对谷歌地图API的支持,但没有添加该功能的时间表
/********************************************************************************
* directionsAPI: get distance and time taking traffic into account, from Google Maps API
********************************************************************************/
function directionsAPI(origin, destination, customDate) {
var Your_API_KEY = "<put your APK key here between the quotes>";
var serviceUrl = "https://maps.googleapis.com/maps/api/directions/json?origin="+origin+"&destination="+destination+"&departure_time="+customDate.getTime()+"&mode="+Maps.DirectionFinder.Mode.DRIVING+"&key="+Your_API_KEY;
var options={
muteHttpExceptions:true,
contentType: "application/json",
};
var response=UrlFetchApp.fetch(serviceUrl, options);
if(response.getResponseCode() == 200) {
var directions = JSON.parse(response.getContentText());
if (directions !== null){
return directions;
}
}
Logger.log("Error: " + response.getResponseCode() + " From: " + origin + ", To: " + destination + ", customDate: " + customDate + ", customDate.getTime(): " + customDate.getTime() );
return false;
}
/********************************************************************************
*directionsAPI:从谷歌地图API获取考虑流量的距离和时间
********************************************************************************/
函数方向API(起点、终点、自定义日期){
var Your_API_KEY=“”;
var serviceUrl=”https://maps.googleapis.com/maps/api/directions/json?origin=“+origin+”&destination=“+destination+”&destination_time=“+customDate.getTime()+”&mode=“+Maps.DirectionFinder.mode.DRIVING+”&key=“+Your_API_key;
var期权={
muteHttpExceptions:true,
contentType:“应用程序/json”,
};
var response=UrlFetchApp.fetch(serviceUrl,选项);
if(response.getResponseCode()==200){
var directions=JSON.parse(response.getContentText());
如果(方向!==null){
返回方向;
}
}
Logger.log(“错误:“+response.getResponseCode()+”从:“+origin+”,到:“+destination+”,customDate:“+customDate+”,customDate.getTime():“+customDate.getTime());
返回false;
}
您应该能够在Google开发者控制台下跟踪配额使用情况。当您启用高级Google服务时,它应该已经创建了一个ProjectId。以下是应用程序脚本的配额限制:我从我的用户那里得到类似的报告@Rivero:1)当我转到开发者控制台(使用电子表格中的链接)时,控制台中没有记录使用情况。我已经启用了方向API。保罗:不需要启用高级谷歌服务器