代码示例:
function getDirections() { var start = 'San Francisco, CA'; var destination = 'Los Angeles, CA'; var mode = 'driving'; var waypoints = 'optimize:true|San Jose, CA|Santa Cruz, CA';
var url = 'https://maps.googleapis.com/maps/api/directions/json?' + 'origin=' + start + '&' + 'destination=' + destination + '&' + 'mode=' + mode + '&' + 'waypoints=' + waypoints + '&' + 'key=' + YOUR_API_KEY;
var response = UrlFetchApp.fetch(url); var directions = JSON.parse(response.getContentText());
if (directions.status == 'OK') { var legs = directions.routes[0].legs; for (var i = 0; i < legs.length; i++) { var steps = legs[i].steps; for (var j = 0; j < steps.length; j++) { var step = steps[j]; var instructions = step.html_instructions; var distance = step.distance.text; Logger.log(instructions + ' (' + distance + ')'); } } } else { Logger.log('Directions service returned an error: ' + directions.error_message); } }