Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

google maps - Call parameter through a webapp?

The code below gets the distance between 2 different addresses and returns the distance in miles, kilometers or minutes. My understanding was that I could execute my app-script in a web app using google.script.run.googleMaps();

My question is how do I use input fields as my from and two columns to replicate the same result in a web app?

function googleMaps(start_address,end_address, return_type){

   var mapObj = Maps.newDirectionFinder();
   mapObj.setOrigin(start_address);
   mapObj.setDestination(end_address);
   var directions = mapObj.getDirections();

   var getTheLeg = directions["routes"][0]["legs"][0];

   var meters = getTheLeg["distance"]["value"];

  switch(return_type){
    case "miles" :
      return Math.floor(meters * 0.000621371);
      break;
    case "kilometers" :
      return meters / 1000;
      break;
    case "minutes" :
      var duration = getTheLeg["duration"]["value"];
      return duration / 60;
      break;
    default :
      return "Error: Wrong unit type";
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Passing parameters to a WebApp

  • A WebApp must contian a function doGet(e) (or alternatively doPost(e)), which will be the first function to be run when the WebApp is called
  • When you deploy a WebApp, the WebApp URL will be something like this: https://script.google.com/a/XXXXX.eu/macros/s/XXXXXXXXXXXXXXXXXXX/exec

  • If you want to pass paramters to the url, you should do it like https://script.google.com/a/XXXXX.eu/macros/s/XXXXXXXXXXXXXXXXXXX/exec?start_address=AAA&end_address=BBB&return_type=CCC

  • Those parameters can be retrieved within the WebApp as
function doGet(e){
 var start_address = e.parameter.start_address;
 var end_address = e.parameter.end_address;
 var return_type = e.parameter.return_type;
 ...
}
  • Now, you can completely integrate your function within doGet() - you do not need google.script.run.googleMaps(); for it

  • If you do want to call your function with google.script.run, note that it is a method that calls an Apps Script function from within client-side (Javascript)
  • If you want to use it, it means that first you need to create a client-side with HtmlService
  • Second, you will need to pass the retrieved parameters from Apps Script to Javascript and back

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...