This is a copy of my "Get directions" script. Check out the transformedSteps array as an example.
The model can contain any objects so you can pass an array into the script by simply binding the array to a variable.
---
function execute(model) {
var mode = Maps.DirectionFinder.Mode.DRIVING;
var travelMode = model.directionsMode;
var depart = new Date();
if ( model.directionsDeparture ) {
var dateString = model.directionsDeparture.replace(/-/g, '/');
depart = new Date(dateString);
}
if ( travelMode ) {
mode = travelMode.toLowerCase();
}
var directions = Maps.newDirectionFinder()
.setOrigin(model.directionsOrigin)
.setDestination(model.directionsDestination)
.setDepart(depart)
.setMode(mode)
.getDirections();
var transformedSteps = [];
var route = directions.routes[0];
if ( route ) {
if ( route.legs ) {
var leg = route.legs[0];
if ( leg ) {
var duration = leg.duration.text;
var distance = leg.distance.text;
var steps = leg.steps;
if ( steps ) {
for ( var i = 0; i < steps.length; i++ ) {
var step = steps[i];
var latlng = model.directionsDestination;
if ( i < steps.length ) {
latlng = dec2StrLat(step.end_location.lat)+","+dec2StrLng(step.end_location.lng);
}
transformedSteps.push({ instructions : step.html_instructions, distance : step.distance.text, duration : step.duration.text, location : latlng, travelMode : step.travel_mode });
}
}
}
}
}
return { directionsDistance : distance, directionsDuration : duration, directionsRoute : route, directionsSteps : transformedSteps };
}
function dec2StrLat(decLatitude) {
var intDegree;
var decMinute;
var strLatitude = "N";
if (decLatitude < 0) {
strLatitude = "S";
decLatitude = decLatitude * -1;
}
intDegree = Math.floor(decLatitude);
decMinute = (decLatitude - intDegree) * 60;
decMinute = Math.round(decMinute*1000)/1000;
strLatitude = strLatitude + " " + String(intDegree) +"° " + String(decMinute) +"'";
return strLatitude
}
function dec2StrLng(decLongitude) {
var intDegree
var decMinute
strLongitude = "E";
if (decLongitude < 0) {
strLongitude = "W";
decLongitude = decLongitude * -1;
}
intDegree = Math.floor(decLongitude);
decMinute = (decLongitude - intDegree) * 60;
decMinute = Math.round(decMinute*1000)/1000;
strLongitude = strLongitude + " " + String(intDegree) +"° " + String(decMinute) +"'";
return strLongitude
}
function getInputParameters() {
var SINGLE_VALUE = 0;
var LIST = 1;
var CONDITION = 2;
return [
{ name : 'directionsOrigin',
description : 'The origin of the directions',
type : SINGLE_VALUE
},
{ name : 'directionsDestination',
description : 'The destination of the directions',
type : SINGLE_VALUE
},
{ name : 'directionsMode',
description : 'The travel mode can be driving, walking, bicycling or transit',
type : SINGLE_VALUE
},
{ name : 'directionsDeparture',
description : 'The departure time, required when using transit mode',
type : SINGLE_VALUE
}
];
}
function getOutputParameters() {
var SINGLE_VALUE = 0;
var LIST = 1;
var CONDITION = 2;
return [
{ name : 'directionsDistance',
description : 'The total distance',
type : SINGLE_VALUE
},
{ name : 'directionsDuration',
description : 'The total duration',
type : SINGLE_VALUE
},
{ name : 'directionsRoute',
description : 'The complete route object - for experts!',
type : SINGLE_VALUE
},
{ name : 'directionsSteps.instructions',
description : 'The driving instructions for each step of the route',
type : LIST
},
{ name : 'directionsSteps.distance',
description : 'The distance for each step of the route',
type : LIST
},
{ name : 'directionsSteps.duration',
description : 'The duration for each step of the route',
type : LIST
},
{ name : 'directionsSteps.location',
description : 'The location for each step to be rendered as a map',
type : LIST
},
{ name : 'directionsSteps.travelMode',
description : 'The travel mode for each step (e.g. WALKING,DRIVING,TRANSIT)',
type : LIST
}
];
}
function doGet(request) {
try {
var command = request.parameters.cmd;
var result = { error : 'Invalid script invokation!' };
if (typeof command != 'undefined' ) {
if ( command == 'ip' ) {
result = getInputParameters();
} else if ( command == 'op' ) {
result = getOutputParameters();
} else {
return HtmlService.createHtmlOutput('<html><body style="height:500px;background-image:url(
http://www.ultradox.com/ultradoxBg.png);background-repeat: no-repeat;background-position: right top;"><table cellspacing="25px" width="450px"><tr><td rowspan="2"><img src="
http://www.ultradox.com/ultradoxOk.png"></td><td style="font:22px Ubuntu">Access granted</td></tr><tr valign="top"><td style="color:#999;font:16px Ubuntu">You can now close this window and reload the script.</td></tr></table></body></html>');
}
}
var json = JSON.stringify(result);
return ContentService.createTextOutput(json).setMimeType(ContentService.MimeType.JSON);
} catch ( err ) {
var msg = err;
if ( typeof err == Error ) {
msg = err.message;
}
return ContentService.createTextOutput("{ __error : '"+msg+"' }").setMimeType(ContentService.MimeType.JSON);
}
}
function doPost(request) {
try {
var json = request.postData.getDataAsString();
var model = JSON.parse(json);
var result = execute(model);
var json = JSON.stringify(result);
return ContentService.createTextOutput(json).setMimeType(ContentService.MimeType.JSON);
} catch ( err ) {
var msg = err;
if ( typeof err == Error ) {
msg = err.message;
}
return ContentService.createTextOutput("{ __error : '"+msg+"' }").setMimeType(ContentService.MimeType.JSON);
}
}