从形成的Google Apps脚本访问地图

时间:2018-03-19 21:47:08

标签: google-apps-script maps

我正在尝试从绑定到Google表单的脚​​本访问地图。我遇到的问题是,在调试脚本时,它经常访问地图,以至于我遇到了配额限制。我有一个Maps API密钥但没有客户端ID,因此无法使Maps.setAuthenication(clientID,Key)生效。我正在为侦察兵部队做这个,所以不想付钱去访问地图。

有人可以帮忙吗?

随后我被要求发布我的代码,所以这里是:

 function setLocation(){
     var whereString;
     var theDuration;
     var theDistance;
     var theRoute;
     var theDirections;
     var theTravelString; 

     // this Sets the Where: Tab on the form
     whereString = 'Where: ' + gLocation;
     theItemArray = gSignupForm.getItems();
     theItemArray[kWhereItem].setTitle(whereString);
     //this gets the directions to the location
     Maps.setAuthentication('','ABCDEFGHIJKLMNOP'); 
     //Obviously Im'm not going to post the true key
     theDirections = Maps.newDirectionFinder()
         .setOrigin('7101 Shadeland Ave, Indianapolis, IN 46256')
         .setDestination(gLocation)
        .setMode(Maps.DirectionFinder.Mode.DRIVING)
        .getDirections();
     theRoute = theDirections.routes[0];
     theDuration = theRoute.legs[0].duration.text;
     theDistance = theRoute.legs[0].distance.text;
     theTravelString = Utilities.formatString('Travel Considerations: The estimated travel distance is %u miles. ',theDistance);
     theTravelString += 'The estimated travel time is ' + theDuration;
     theItemArray[kTravelItem].setTitle(theTravelString);
}

1 个答案:

答案 0 :(得分:0)

限制使用低配额服务的一种解决方案是避免在所需信息未发生变化时调用这些服务。

例如,通过使用CacheService,您可以大大减少对Maps API的调用,无论是否调试会话:

var cache = CacheService.getScriptCache();
function setLocation() {
  // Try to find the route for this location if it's still available
  var storedRoute = cache.get(gLocation);
  if (!storedRoute) {
    // No route for this value of the key gLocation was found. Query as normal.
    ...
    theRoute = theDirections.route[0];
    // Cache this route for future uses, for the maximum of 6hr).
    cache.put(gLocation, JSON.stringify(theRoute), 21600);
  } else {
    // We have this exact stored route! Convert it from the stored string.
    theRoute = JSON.parse(storedRoute);
  }
  theDuration = ...
  ...

您的“gLocation”变量可以直接用作缓存键。如果没有,您需要通过编码使其可用。最大长度键为250个字符。此单参数缓存假定您的路线都具有固定端点,如示例代码所示。如果两个端点都不同,则必须根据这两个值构建缓存键。

相关问题:Maps direction Quota limits