Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps 谷歌地图API,参考错误:谷歌未定义_Google Maps_Google Apps Script_Google Sheets - Fatal编程技术网

Google maps 谷歌地图API,参考错误:谷歌未定义

Google maps 谷歌地图API,参考错误:谷歌未定义,google-maps,google-apps-script,google-sheets,Google Maps,Google Apps Script,Google Sheets,我正在尝试使用下面的函数映射google工作表中两列地址之间的距离。我得到了“google未定义”错误,当在网页上下文中使用时,显然是在网页完全加载之前尝试访问google对象的结果 为什么我会在谷歌表单中出现这个错误?我不允许使用相同的API吗 该错误由getDrivingMiles函数引发 var alert = SpreadsheetApp.getUi().alert var active_sheet = SpreadsheetApp.getActiveSheet(); var direc

我正在尝试使用下面的函数映射google工作表中两列地址之间的距离。我得到了“google未定义”错误,当在网页上下文中使用时,显然是在网页完全加载之前尝试访问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 "";
}
var alert=SpreadsheetApp.getUi().alert
var active_sheet=SpreadsheetApp.getActiveSheet();
var定向服务;
函数onOpen()
{
directionsService=new google.maps.directionsService();
旅行模式=google.maps.directions旅行模式.DRIVING;
alert=SpreadsheetApp.getUi().alert
active_sheet=SpreadsheetApp.getActiveSheet();
选项=[
{
名称:“计算里程”,
函数名:“getDistanceInMiles”
}, ];
激活的工作表。添加菜单(“行驶距离”,选项);
}
函数getColNumberByName(列名称)
{
var整张工作表数据=活动工作表
.getRange(“A1:1”)
.getValues();
var col\u number=整张表数据[0]。indexOf(列名称);
返回(列号)
}
函数getColDataByColname(列名称)
{
var col\u number=getColNumberByName(列名称);
如果(列号!=-1)
{
返回当前工作表
.getRange(2,列编号+1,活动的工作表.getMaxRows())
.getValues();
}
}
函数getRangeByColname(列名称)
{
var col\u number=getColNumberByName(列名称);
如果(列号!=-1)
{
返回活动工作表.getRange(2,列数+1,活动工作表.getMaxRows())
}
}
函数getDistanceInMiles()
{
var origins=getColDataByColname('From Address');
var destinations=getColDataByColname('To Address');
var distance_col=getColNumberByName(“以英里为单位的距离”);
var num_addresses=origins.length;
var origin=“”;
var destination=“”;
var距离=0;
对于(变量i=0;i
尽管谷歌应用程序脚本是Javascript的一个子集,并共享许多功能,但它们的编程方式不同。也就是说,由于在您的应用程序的全局范围内没有声明“google”对象(或默认情况下的任何GAS应用程序),因此在引用它时会返回一个错误

为了从GAS应用程序访问Maps API,您应该使用受支持的内置服务,或者更好地使用。在您的情况下,这将使用

您可以在以下链接中看到更多信息:

  • 。支持对服务的调用
  • 。使用测向仪。包括对您非常有用的示例

“显然在网页上下文中使用时”。您是否在“网页上下文”中使用此脚本?否。在谷歌工作表中。我已加载了您的代码<代码>getDrivingMiles工作正常-无错误。您的问题中列出的任何脚本都没有调用它,它是100%有效的谷歌地图服务-我怀疑这是错误的根源。其他脚本是一个混合体:
getDistanceInMiles
来自此,使用地图API并需要
scripts.html
来调用Google地图API
onOpen
:我是一个如此糟糕的编码员,以至于我根本无法理解它。我的建议是:删除除getDrivingMiles之外的所有内容,这样就可以了。@Tedinoz——这是我首选的方法。然而,我的工作表有这么多地址,尽管有延迟,我还是收到了超时错误。啊,我明白了。我正在复制为网页设计的代码,并试图在应用程序脚本中使用它。不同的接口。