Following up on our recent post about the new Doc List capability in Google Apps Script, we thought we’d take a moment to look a little more closely at another new feature in Apps Script - integration with Google Maps. Google Maps has had an API for quite some time, but now we’ve made it very easy to generate customized maps and driving directions straight from a script.
A mail merge is often used to automate some of the drudgery involved in sending invitations to a large number of people. With the new Apps Script Maps Service you can easily add a map image to the email, and even add a marker showing the event location.
While that’s nice, it’s hardly a time saver - why write code to generate the same image repeatedly? A much more useful feature is generating a custom map for each guest, along with personalized driving directions. We’ve made a spreadsheet template that includes just such a script - it’s available here.
Let’s look at a short code snippet that illustrates the calls required to generate a map image and add a marker at the start and end addresses:
function getMap (start, end) {
// Generate personalized static map.
var directions = Maps.newDirectionFinder()
.setOrigin(start)
.setDestination(end)
.getDirections();
var map = Maps.newStaticMap().setSize(500, 350);
map.setMarkerStyle(Maps.StaticMap.MarkerSize.MID,
"red", null);
map.addMarker(start);
map.addMarker(end);
return map.getMapUrl()
}
Running the function with start and end set to Google, San Francisco and Google, Mountain View, we get a link to the following image:
Along with generating maps images, the Maps feature can also find directions, retrieve elevation data and even perform some geocoding operations. Please note that any data returns by these APIs should not be used without displaying an associated map image - see the Google Maps API Terms of Service for more details.
Posted by Evin Levey, Google Apps Script Product Manager