UrfFetchApp.fetch错误,谷歌地图方向网址带有航路点(航路点以|分隔)

时间:2017-10-28 08:51:38

标签: google-maps google-maps-api-3 google-apps-script urlfetch

我在Google Apps脚本(GAS)上使用Google地图方向。 该程序创建了一个URL:

function createUrl(origin, destination,wayPoint) {
          var Your_API_KEY = "my code here";
          var serviceUrl = "https://maps.googleapis.com/maps/api/directions/json?" 
          +"origin="+origin
          +"&destination="+destination
          +"&waypoints=optimize:true|"+ wayPoint
          +"&mode="+Maps.DirectionFinder.Mode.DRIVING
          +"&alternatives="+Boolean(0)
          +"&key="+Your_API_KEY;
         return(serviceUrl);
        }

并在GAS中使用UrlFetchApp.fetch()来获取结果。

function test () {
          var origin = "6501 Dresden Ln, Raleigh, NC 27612, USA";
          var destination = "5000 Capital Blvd, Raleigh, NC 27616, USA ";
          var items = ["7928 Mandrel Way, Raleigh, NC 27616, USA"];         
          var waypoints = createWayPoint(items);
          var serviceUrl = createUrl(origin,destination,waypoints);
          Logger.log (serviceUrl);
          var options= {
            muteHttpExceptions:true,
            contentType: "application/json",
          };

          var response = UrlFetchApp.fetch(serviceUrl,options); //ERROR HERE
          if(response.getResponseCode() == 200) {
            var directions = JSON.parse(response.getContentText());
            if (directions !== null){
             // do something;
            }
          }
        }

serviceUrl是这样的:

" https://maps.googleapis.com/maps/api/directions/json?origin=6501 Dresden Ln,Raleigh,NC 27612,USA& destination = 5000 Capital Blvd,Raleigh,NC 27616,USA& waypoints = 7928 Mandrel Way,Raleigh,NC 27616,USA | 6501 Dresden Ln,Raleigh,NC 27612& mode = driving& alternatives = false& key = MyKeyCodeHere"

我可以使用此serviceUrl 在浏览器中获取结果手册。但UrlFetchApp.fetch()不能。

我发现如果serviceUrl没有" |",它可以工作。但是我需要多个航路点并用" |"在Url中(我想使用很多航点 - 靠近23个航点 - 所以我需要在Url中)。查看航点:https://developers.google.com/maps/documentation/directions/intro#Waypoints

// multi waypoints separated by |.
    function createWayPoint (arrayWayPoint){
      var waypoints = "";
      for (var i = 0; i < arrayWayPoint.length; i++) {
        var address = arrayWayPoint[i];
        if (address !== "") {
          if (i==0){
            waypoints = arrayWayPoint[0];
          }
          else {
            waypoints = waypoints + " | " + arrayWayPoint[i];
          }
        }
      }
      return waypoints;
    }

有人帮助我吗?非常感谢你。

1 个答案:

答案 0 :(得分:1)

如何将encodeURIComponent()用于以下示例?

示例脚本:

function createUrl(origin, destination,wayPoint) {
  var Your_API_KEY = "my code here";
  var serviceUrl = "https://maps.googleapis.com/maps/api/directions/json?" 
  +"origin="+encodeURIComponent(origin)
  +"&destination="+encodeURIComponent(destination)
  +"&waypoints=" + encodeURIComponent("optimize:true|"+ wayPoint)
  +"&mode="+Maps.DirectionFinder.Mode.DRIVING
  +"&alternatives="+Boolean(0)
  +"&key="+Your_API_KEY;
  return(serviceUrl);
}

如果这不起作用,我很抱歉。